我正在尝试使用以下代码从网站下载图像。
目前它可以识别并下载文件。但是 下载的文件未打开,显示已损坏。以下代码中的问题是什么?
此外,我将此页面嵌入我的一个移动应用程序中。它也适用于移动设备吗
<?php
$file = "http://example.com/animals/1.jpg";
// Parse Info / Get Extension
$fsize = filesize($file);
$path_parts = pathinfo($file);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext)
{
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: die('Wrong Extension');
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"Test".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($file);
exit();
?>
答案 0 :(得分:0)
您需要将数据流发送到浏览器。
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); // change this will work for you.
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;