0

uploading an image with file_put_contents makes 0 byte file.

here is the code that I use. I extract facebook image url and put it into the web server.

$fb_image_url = 'https://example.com/229282.jpg'
    
$filename = substr($fb_image_url, strrpos($fb_image_url, '/') + 1);
    
file_put_contents('upload/user_pic/original/'.$filename, file_get_contents($fb_image_url));

after I do this, the server receives the file name successfully, but it is 0 bytes.

I checked php.ini, and allow_url_fopen is ON.

uploading folder permission is also fine.

Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
james
  • 225
  • 6
  • 20
  • Take a look at [GD library](http://php.net/manual/fr/book.image.php) and function [imagecreatefromstring](http://www.php.net/manual/fr/function.imagecreatefromstring.php) – jbrtrnd May 29 '12 at 10:27
  • Have you checked what's the output of `file_get_contents`? – Leri May 29 '12 at 10:27
  • Try putting `file_get_contents` into a separate line - to receive the content of the file into a variable. Then you can check what you received from the server. You need to isolate the problem: loading or saving. – Aleks G May 29 '12 at 10:28
  • @Aleks G I did what you said, but it didn't change anything. It seems like it receive 0 byte data at least. – james May 29 '12 at 10:38
  • Ok, then at least you know that the problem is with loading data. Next try opening the URL in question from a browser that **does NOT** have an active facebook session (i.e. log out of facebook). It's possible that facebook requires an active session before allowing you to download images. If that is the case, check out "Example #4 Using stream contexts" example on http://php.net/manual/en/function.file-get-contents.php – Aleks G May 29 '12 at 10:39
  • I found the best way to do that. Write the request ! See : [http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents][1] [1]: http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents – Macbric Sep 12 '13 at 14:04

3 Answers3

1

I just put that URL into the browser:

https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/274661_1171545457_6475606_n.jpg

And received 404 Not Found in response. This would explain why you get empty file locally. I strongly suggest that you load the data first, verify what you received and then, if validation passes, save it locally.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I didn't want to show valid jpg file on public. it is an example image. – james May 29 '12 at 10:33
  • In any event, always load the file into a variable first, deal with different exceptions (not found, permission denied, etc.) - then, if you're happy, save locally. – Aleks G May 29 '12 at 10:35
1

To copy images from facebook, script/php program requires permission for the same. if the program/ FB API doesn't pass the validation/permission check, FB doesn't allow downloading of any image.
It looks like your Application doesn't have permission to download/copy this image from Facebook that's why you're getting 0 bytes. Try giving PUBLIC access to images and keep FB account logged in while coping image

Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
Ganesh Bora
  • 1,133
  • 9
  • 17
0

here is the solution. I had a problem with facebook profile address.

$fb_image_url = 'https://graph.facebook.com/'.$facebook_id.'/picture?type=large';

$filename = $fb_id.'_'.time().'.jpg';

$result = file_get_contents($fb_image_url);

file_put_contents('upload/user_pic/original/'.$filename, $result);

with this_ it worked fine. Yeah! Thank you everyone!

james
  • 225
  • 6
  • 20