我使用下面的代码将图像从url复制到我的web目录的功能,但我不知道为什么有些图像不能100%复制。从url复制后不是精确的图像复制
这里是我的web目录http://www.aligatoor.pl/uploads/deals/2632b3802e8ed7abe83788dab605bbf7.jpg
错图像的副本,这里的原始图像http://infobuzer.pl/img/r4d3k/voucher/50e35a84c9038.jpg
function upload_file($file_path)
{
$deal_img_data=file_get_contents($file_path);
$file_name=md5($file_path).".jpg";
if(!file_exists($file_name))
{
$file=fopen($file_name,"w+");
fwrite($file,$deal_img_data);
}
}
我也试过,卷曲,但它仍然无法正常复制一些图片。
卷曲代码:
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
$saveto=md5($url).".jpg";
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
虽然不只是file_put_contents而不是fopen/fwrite? – 2013-02-14 21:45:04
您可能只需指定“wb +”作为标志。 b表示二进制模式。这可能是原因。它可能认为它是一个文本文件。 – 2013-02-14 21:46:52
“wb +”和file_put_contents不起作用到 – Marek 2013-02-14 22:16:24