1

I need to download some images from a specific array of urls, something like:

<?php
  $images = array('http://url1.com/img1.png','http://url1.com/img2.png'); 
   // download this images from this paths

I saw some scripts here but don't get them exactly on how can i link them to this array.

The expected output would be: When i run the script to download those images from that array to a specific folder from my server: home/user/public_html/images. It's much appreciated anyone who helps me, i am trying but can't make the connections, currently a rookie.

user3140607
  • 303
  • 4
  • 19
  • 1
    `foreach()` loop, then what ever code to process one at a time –  Jan 15 '14 at 19:35
  • duplicate http://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php – MrHunter Jan 15 '14 at 19:36
  • can we see your script? – Alireza Fallah Jan 15 '14 at 19:38
  • @Dagon more specifically? – user3140607 Jan 15 '14 at 19:39
  • @MrHunter It;s not duplicate refers to a specific thing (Download from SPECIFIC array). I searched a lot before posting the question. – user3140607 Jan 15 '14 at 19:40
  • you said you had code to do one url, so all you need is to put that inside a foreach loop –  Jan 15 '14 at 19:41
  • @AlirezaFallah That's the problem, i saw lots of scripts here but don't really no exactly how to connect to this, because all of them are about how to take from a single path of a known absolute path of a remote server or different then what i need. – user3140607 Jan 15 '14 at 19:41
  • **Try writing something yourself** and then if it doesn't work, show us specifically what you did so we can help you along. You start it, we help. We don't write it for you. Show us the actual code that you've tried and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Jan 15 '14 at 19:43

1 Answers1

1

Something like this maybe.

$images = array('http://ecx.images-amazon.com/images/I/214RgVjsvTL.jpg','http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg'); 

foreach($images as $name=>$image) {

    //get image
    $imageData = file_get_contents($image); //$image variable is the url from your array

    $name = explode("/", $image);

    $handle = fopen("images/".$name[5],"x+");     

    fwrite($handle,$imageData);

    fclose($handle);
}
mituw16
  • 5,126
  • 3
  • 23
  • 48
  • I get `file_get_contents(http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg ) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request` – user3140607 Jan 15 '14 at 19:52
  • http://www.testingcom.tk/test.php here is the testing and the contents: http://codepad.org/e7zp7UdC. Hope it's not like a forbidden error from amazon. – user3140607 Jan 15 '14 at 19:59
  • @user3140607 You have a newline at the end of the URLS in your code. Remove those and try again. – mcrumley Jan 15 '14 at 20:06
  • I can see that it makes some empty files `$handle = fopen("images/image".$count.".png","x+");` the fact is that i need the exact name of the image from the specified url. Example: the url is http://amazon.com/imagexyz.png to get downloaded with that name: imagexyz.png, so no need for a contor. – user3140607 Jan 15 '14 at 20:10
  • It downloads them perfectly excepting the name, how can i get the exact name, instead of image1... – user3140607 Jan 15 '14 at 20:18
  • I see, so this is the only way :( Would have been great to detect after 5 slashes to copy the name directly. – user3140607 Jan 15 '14 at 20:25
  • @user3140607 I updated my answer to use `explode`. Hope that helps! :)http://us1.php.net/explode – mituw16 Jan 15 '14 at 20:30
  • `$name = explode('/', $image); $name = end($name);` or `$name = basename($image);` works without relying on the number of path segments being constant. – mcrumley Jan 15 '14 at 20:46