3

我正在尝试使用 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;
    }




    ?>
4

2 回答 2

7
<?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);     
?>
于 2014-08-12T06:18:19.830 回答
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% :)

于 2013-11-04T18:50:05.903 回答