2

I have one Image on another server (Image).but when i get this image With file_get_contents() function it will return

Not Found Error

and generate this Image.

file_put_contents(destination_path, file_get_contents(another_server_path));

plz help me. if there are another way to get those image.

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46

2 Answers2

1

Try this.

There is problem with URL Special character.then you have to decode some special character from url basename.

$imgfile = 'http://www.lagrolla.com.au/image/m fr 137 group.jpg';
$destinationPath = '/path/to/folder/';
$filename = basename($imgpath);
$imgpath = str_replace($filename,'',$imgpath).rawurldecode($filename);
copy($imgfile,$destination_path.$filename);
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46
0

Another way to download copy file from another server is using curl:

$ch = curl_init('http://www.lagrolla.com.au/image/data/m%20fr%20137%20group.jpg');
$destinationPath = '/path/to/folder/filenameWithNoSpaces.jpg';
$fp = fopen($destinationPath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Note: It is bad practice to save images with spaces in file name, so you should save this file with proper name.

aslawin
  • 1,981
  • 16
  • 22
  • dear there no problem with file name, because file name set to returned content but it will return not found page so .. – Bhavik Hirani Sep 02 '16 at 07:59
  • Right, its only some note, and I know that's not an issue. What happened when you trying to download this file using `curl`? – aslawin Sep 02 '16 at 08:02