How can get an image from a 301 redirect download link in PHP? How can get an image from a 301 redirect download link in PHP? curl curl

How can get an image from a 301 redirect download link in PHP?


$curl = curl_init("http://minecraft.net/skin/Notch.png");// Moved? Fear not, we'll chase it!curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);// Because you want the result as a stringcurl_setopt($curl,  CURLOPT_RETURNTRANSFER, true); $bin = curl_exec($curl);curl_close($curl);$img = @imagecreatefromstring($bin);


Here is an option to directly save the image to a file (instead of using imagecreatefromstring):

<?php$fileName = '/some/local/path/image.jpg';$fileUrl  = 'http://remote.server/download/link';$ch = curl_init($fileUrl); // set the url to open and download$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded imagecurl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointercurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirectscurl_exec($ch); // fetch the image and save it with curlcurl_close($ch); // close curlfclose($fp); // close the local file pointer


fopen - depends on your php settings if url fopen is allowed.

or curl

see the fine php manual.