简体   繁体   中英

How can I fetch/save image from dynamic url in PHP

How can I fetch/save image from dynamic url like this?

http://www.groove3.com/str/image.php?type=P&id=16470

I am using function below but it doesn't work on dynamic urls:

function saveImage($image_url,$save_to){
    if(!file_exists($save_to) && $image_url != "")
    {
        $ch = curl_init($image_url);
        $fp = fopen($save_to, 'wb');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL,$image_url);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp); 
    }       

}

You can use file_get_contents instead.

$img_content = file_get_contents($image_url);
//Save the file in file system.
$fp = fopen($save_to, "w");
fwrite($fp, $img_content);
fclose($fp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM