欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php 下载 打开 文件 图片 图像 有大用 有大大用 有大大大用

shiping1 的头像

1)下载
<?php

/**
 * download page
 *
 * @author  shipingzhong <shipingzhong@altech-it.cn>
 * @version 20090525
 */
 
session_cache_limiter("public"); // for IE

// read user common file
//include this file.... and it has smarty function
require_once("./include/User.inc.php");
   
$uid = $_SESSION['sess_marry_user']['user_id'];
$wid = $_SESSION['sess_marry_user']['wedding_id'];
//download?dl=1&pid={$dataRow.photo_id}"
 
if (empty($uid))
{
    header("location :".HTTP_URL);
}

$pid             = $_GET['pid'];

$userIdInImage   = $Download->getUserIdInImage($pid);

$dataRow         = $Photo->getOne($pid, $userIdInImage);
 
//session_start();              // session_start() in here is error .I don not know
 
$file_name       = $dataRow['photo_real_name']."." .$dataRow['extension'];
 
$uid             = $_SESSION['sess_marry_user']['user_id'];

$file_path       = PHOTO_PATH . $wid . "/" . $file_name;

$showFileName    = $dataRow['photo_base_name'];
$showFileName    = mb_convert_encoding($showFileName, "SJIS", "UTF-8");

Header("Content-type: application/octet-stream");
//Header("Content-type: application/txt");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_path));
//把 attachment 改成 inline 应该就是打开 好像不行 我暂时也不懂inline什么意思
//Header("Content-Disposition: attachment; filename=" . $showFileName);
Header("Content-Disposition: attachment; filename=\"$showFileName\"");
//殷
 $fp=fopen($file_path, "r");//非文本文件最好用 "rb"

 echo fread($fp, filesize($file_path));
 fclose($fp);
//
//exit;
?>


下载第二个例子 

<?php

if(isset($_GET['url'])){ 

$filename=$_GET['url'];//获取参数 

header('Content-type: image/jpeg'); 

header("Content-Disposition: attachment; filename=eweimanet".date('YmdHis').rand(100,999).".jpg"); 

readfile($filename);

//注意:header函数前确保没有任何输出 


exit;//结束程序 

}

?>



2)打开文件

<?php
/*
 * Created on 2017-3-21
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 $name = 'aaa.jpg';
 $path = 'aaa.jpg';
 switch(substr(strrchr($name, '.'), 1))
{
    case 'jpeg': case 'JPEG':   
    case 'jpg':  case 'JPG':    $type = "Content-type: image/jpg";  break;
    case 'gif':  case 'GIF':    $type = "Content-type: image/gif";  break;
    case 'png':  case 'PNG':    $type = "Content-type: image/png";  break;
    default:                    $type = "Content-type: application/octet-stream";   break;
}
 
 //Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
//header("Content-Description: File Transfer");
//Use the switch-generated Content-Type
header("Content-Type: " . $type);
//Force the download
header("Content-Disposition: inline; filename=" . $name . ";");
header("Content-Transfer-Encoding: binary");
//header("Content-Length: " . filesize($path));
// read
echo @readfile($path);
 
exit();
 
?>
 


普通分类: