4

This may be a strange question, but I have this loop here:

foreach ($friends["data"] as $value) {
    echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />
}

Running this opens over 2000 images.

I need to download each of these images to a folder on my desktop with the name 1.jpg, 2.jpg, 3.jpg, etc.

Frankly this is weird to me, and I don't even know how to try and research how to do this. This images aren't huge, but the do need to stay in order as they will be lined up later with captions that are numbered.

Any help greatly appreciated!

I tried this:

foreach ($friends["data"] as $value) {

        $i = 1;

        echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/> <br />'; 
        $url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
        $img = '/me/Desktop/Facebook/'.$i.'.jpg';
        file_put_contents($img, file_get_contents($url));
        $i++;



    }

but I got the error: Warning: file_put_contents(/me/Desktop/Facebook/1.jpg) [function.file-put-contents]: failed to open stream: No such file or directory in /home/blahblah blah on line 125

Joel
  • 2,691
  • 7
  • 40
  • 72
  • You could choose two ways to do something like that: 1st) Use Javascript to download all those images to your CLIENT. 2nd) In your PHP script collect all the images and put them in a zip file and offer this one file to download. – Eggplant Sep 03 '13 at 08:53
  • I like the idea of makign it a download. I see this: http://php.net/manual/en/function.copy.php How do you determine the destination? – Joel Sep 03 '13 at 09:02
  • Wait, is *your desktop* on the same machine where this PHP script is running? I thought you wanted this to be run on a server and then download it to a client. Please could you be more clear about this? – Eggplant Sep 03 '13 at 09:05
  • The problem in your script is that you are trying to write in a folder that the server cannot read/write because of permissions, try @TaeL answer's code or you can download it using [curl](http://www.php.net/manual/en/function.curl-init.php) – Sal00m Sep 03 '13 at 09:05
  • running on the server and downloading to client after is fine-but I'd like to save all the images in one zip folder for easier download. – Joel Sep 03 '13 at 09:12

1 Answers1

2

looks easy.

$i = 1;
foreach ($friends["data"] as $value) {
    $url = 'https://graph.facebook.com/' . $value["id"] . '/picture?type=large';
    copy($url,"$i.jpg");
    $i++;
}

check your directory placed 1.jpg, 2.jpg, 3.jpg ...

TaeL
  • 1,026
  • 9
  • 17
  • hmm- I got this error and only got the first 150 images: Fatal error: Maximum execution time of 30 seconds exceeded in /home/xxx/public_html/example.com/facebooksdk/facebook-php-sdk/examples/test.php on line 135 – Joel Sep 03 '13 at 09:19
  • 2
    This is why your code is executing for more than 30 seconds (lots of images i suppose :)). Try to paginate, or if you cannot do this, increment the execution time using this in the beginning of your script: `ini_set('max_execution_time', 600);` (600 seconds are 10 minutes i think is enough), but i think its better to refine your script using pagination – Sal00m Sep 03 '13 at 09:23
  • Thank you. I just found that here too- http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-exceeded – Joel Sep 03 '13 at 09:24
  • 1
    It's running now...seems to be working well, but will wait till it's done to give check mark. – Joel Sep 03 '13 at 09:36
  • 1
    Well, the files named themselves, but I just downloaded a couple and got "The image “view-source:http://example.com/facebooksdk/facebook-php-sdk/examples/photos/1005.jpg” cannot be displayed because it contains errors." and the images all have 0kb. – Joel Sep 03 '13 at 09:51