I want to copy an image from an another server, but it doesn't work and i don't know why. Here is my code:
if(copy('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg', 'zzz.jpg')) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
It always returns failed.
You are using the copy
function. Although this method works with both remote sources and destinations, you are encouraged to use the file_put/get_contents
-methods (see documentation quote below).
$image = file_get_contents('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg');
file_put_contents('zzz.jpg', $image);
From the file_get_contents
documentation:
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Also note:
A URL can be used as a filename with this function if the fopen wrappers have been enabled.
However, if fopen
remote URL is not turned on in your settings, you might test with cURL:
$ch = curl_init('http://demo.swyp.fr/mod_traffiq/thumb/LQ1009C/LQ143559C/LQ157553C-71x100.jpg');
$fp = fopen('zzz.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
There could be other PHP settings (or server configurations) which disables all of these code snippets from running. If this does not work, then it is a matter of configuration issues instead.