PHP下载图像脚本不起作用
我正在使用下面的代码将图像下载到我的硬盘上。通过使用我在新闻页面中放置的第一个链接,我可以下载图像。我放在图库中的第二个链接不起作用。路径是正确的,我打开了一个没有downlaod.php文件的图像,路径中没有错误,但是图像不是downlaoding。我使用不同的图像id尝试了不同的图像,但在特定页面上不起作用PHP下载图像脚本不起作用,php,Php,我正在使用下面的代码将图像下载到我的硬盘上。通过使用我在新闻页面中放置的第一个链接,我可以下载图像。我放在图库中的第二个链接不起作用。路径是正确的,我打开了一个没有downlaod.php文件的图像,路径中没有错误,但是图像不是downlaoding。我使用不同的图像id尝试了不同的图像,但在特定页面上不起作用 <a href="<?php echo SITE; ?>download.php?filename=<?php echo SITE; ?>uploads/n
<a href="<?php echo SITE; ?>download.php?filename=<?php echo SITE; ?>uploads/news/<?php echo $rowimg['image']; ?>" title="download">
错误是:文件可能已损坏或太大
下载.php
<?php
$filename = $_GET["filename"];
$buffer = file_get_contents($filename);
/* Force download dialog... */
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Type: application/image");
/* Don't allow caching... */
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
/* Set data type, size and filename */
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($buffer));
header("Content-Disposition: attachment; filename=$filename");
/* Send our file... */
echo $buffer;
?>
使用cURL
$ch = curl_init('http://my.image.url/photo.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // buffer output (`true` if you will be redirecting stream to the user, in $result will be your image content)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // because image is binary data
// Within these two lines you can donwload image directly to the file on your server
// $fp = fopen('/my/saved/image.file', 'w');
// curl_setopt($ch, CURLOPT_FILE, $fp);
$result = curl_exec($ch); // executing requrest (here will be raw image data if RETURNTRANSFER == true)
$response_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); // getting response code to ensure that request was successfull
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); // getting return content-type
// Listing allowed image types
$allowed_mimes = array(
'image/jpeg',
'image/gif',
'image/png'
);
if ($response_code == 200 AND in_array($content_type, $allowed_mimes) ) {
header( 'Content-Type: '.$content_type );
echo $result; // echoing image to STDOUT
} else
return false;
我不确定错误是什么,但我看到了一个巨大的安全整体,如果我这样做了会怎么样。。那么,这个图像有多大p您可以尝试将“\r\n”添加到所有标题标题(“内容类型:应用程序/octet流\r\n”)你是对的,我没注意到。。谢谢有没有只下载图像的代码?@Matthias这是120kb的图像。。
$ch = curl_init('http://my.image.url/photo.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // buffer output (`true` if you will be redirecting stream to the user, in $result will be your image content)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // because image is binary data
// Within these two lines you can donwload image directly to the file on your server
// $fp = fopen('/my/saved/image.file', 'w');
// curl_setopt($ch, CURLOPT_FILE, $fp);
$result = curl_exec($ch); // executing requrest (here will be raw image data if RETURNTRANSFER == true)
$response_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); // getting response code to ensure that request was successfull
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); // getting return content-type
// Listing allowed image types
$allowed_mimes = array(
'image/jpeg',
'image/gif',
'image/png'
);
if ($response_code == 200 AND in_array($content_type, $allowed_mimes) ) {
header( 'Content-Type: '.$content_type );
echo $result; // echoing image to STDOUT
} else
return false;