我正在尝试使用php curl将远程图像保存到文件,但该文件永远不会被保存!任何人都可以帮我解决这个问题吗?图像文件永远不会被创建,但echo $ returned_content有数据!
<?
$returned_content = get_data('http://somesite.com/43534545345dfsdfdsfdsfds.jpg');
echo $returned_content;
$fp = fopen('43534545345dfsdfdsfdsfds.jpg', 'w');
fwrite($fp, $returned_content);
fclose($fp);
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
答案 0 :(得分:6)
<?php
$ch = curl_init('http://www.example/2012/09/flower.jpg');
$fp = fopen('/localProject/imagesFolder/newname.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
答案 1 :(得分:0)
试试这个
<?php
function getImagen($url, $rename, $ch)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
}
$ch = curl_init();
$image ="http://sampleurl.com/imagen.jpg";
getImagen ($image, "imagen", $ch);
?>
100%工作:)