php读取图片或其它文件流输出到页面图片或文件的方法
浏览数 191626
赞
(0)
1、下载文件
$file_path='文件路径';
header("Content-Type: application/octet-stream");
$data = fopen($file_path, 'rb');
while (!feof($data)) {
echo @fread($data, 8192);
flush();
ob_flush();
}
fclose($data);
exit;
2、在浏览器直接查看图片或文件
方法一:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
// header('content-type:image/jpg;'); //直接写死文件为jpg图片
header("Content-type: {$size['mime']}");
$data = fopen($file_path, 'rb');
while (!feof($data)) {
echo @fread($data, 8192);
flush();
ob_flush();
}
fclose($data);
exit;
方法二:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
header("Content-type: {$size['mime']}");
$content=file_get_contents($file_path);
echo $content;
exit;
方法三:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
$fp=fopen($file_path, "rb"); //二进制方式打开文件
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp); // 输出至浏览器
exit;
} else {
// error
}
3、PHP从远程服务器下载文件
function downloadFile($file_path){
$pathinfo = pathinfo($file_path);
is_dir($pathinfo['dirname'].'/') or mkdir($pathinfo['dirname'].'/', 777, true);
$handle=fopen($file_path,'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/text.jpg');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE,$handle); //将文件流直接写入文件句柄,节省内存空间
$result=curl_exec($ch);
curl_close($ch);
fclose($handle);
if ($result) {
return true;
}
}
$file_path='文件路径';
header("Content-Type: application/octet-stream");
$data = fopen($file_path, 'rb');
while (!feof($data)) {
echo @fread($data, 8192);
flush();
ob_flush();
}
fclose($data);
exit;
2、在浏览器直接查看图片或文件
方法一:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
// header('content-type:image/jpg;'); //直接写死文件为jpg图片
header("Content-type: {$size['mime']}");
$data = fopen($file_path, 'rb');
while (!feof($data)) {
echo @fread($data, 8192);
flush();
ob_flush();
}
fclose($data);
exit;
方法二:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
header("Content-type: {$size['mime']}");
$content=file_get_contents($file_path);
echo $content;
exit;
方法三:
$file_path='文件路径';
ob_clean(); //清空之前输出到缓存里的数据
$size = getimagesize($file_path); //获取mime信息
$fp=fopen($file_path, "rb"); //二进制方式打开文件
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp); // 输出至浏览器
exit;
} else {
// error
}
3、PHP从远程服务器下载文件
function downloadFile($file_path){
$pathinfo = pathinfo($file_path);
is_dir($pathinfo['dirname'].'/') or mkdir($pathinfo['dirname'].'/', 777, true);
$handle=fopen($file_path,'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/text.jpg');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE,$handle); //将文件流直接写入文件句柄,节省内存空间
$result=curl_exec($ch);
curl_close($ch);
fclose($handle);
if ($result) {
return true;
}
}