Bhavik Hirani
Bhavik Hirani

Reputation: 2016

Get Image With file_get_contents it return Not Found Error

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.

Upvotes: 2

Views: 1430

Answers (2)

user6739582
user6739582

Reputation:

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);

Upvotes: 1

aslawin
aslawin

Reputation: 1981

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.

Upvotes: 0

Related Questions