我编写此脚本以供测试下载文件。
<?php
$txt = "http://www.branded3.com/wp-content/uploads/2011/05/Google_Chrome1.jpg";
$img = "01.jpg";
file_put_contents($img, file_get_contents($txt));
?>
我在安装了xampp的计算机上运行脚本,它绝对有效。
但是我在另一台计算机上运行它,而不是工作。 在这个问题上有人可以帮助我吗?
答案 0 :(得分:1)
这是一种非常粗糙的方式,您可以代理图像并提示下载。
<?php
$url = "http://www.branded3.com/wp-content/uploads/2011/05/Google_Chrome1.jpg";
//Get file
$source = file_get_contents($url);
//Image Mime types
$images = array('jpg'=>'image/jpg','png'=>'image/png','png'=>'image/png');
//Is it an image extention
if(in_array(substr($url,-3),$images)){
$type = $images[substr($url,-3)];
}else{
//No its somthing else
$type = 'application/octet-stream';
}
//Set the headers
header('Content-Description: File Transfer');
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename='.basename($url));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . sprintf("%u", strlen($source)));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
//echo the source
echo $source;
?>