-1

I have following code:

  $url = explode('\n', $urls);
  $count = 0;
  foreach($url as $image) {
  $img = 'c://wamp/www/www.mysite.com/uploads/images/cat1/image'.$count++.'.png';
    file_put_contents($img, $image);
    }

In variable $urls have many urls with images in format .png.

I try created foreach for each new image create, for example: image1.png, image2.png. It does not work:

Thanks for any help

Gislef
  • 1,555
  • 3
  • 14
  • 37
  • *It does not work* - What doesn't work? Is it not counting properly? Are you getting an error? – matt. Jan 04 '16 at 20:31
  • 1
    This creates the image files named with the count properly. But the images are empty when opened. I tried the suggestions below and also failed yet – Gislef Jan 04 '16 at 20:37

2 Answers2

2

If you want to save the image data in the file, you will need to retrieve it from the url:

 $url = explode('\n', $urls);
  $count = 0;
  foreach($url as $image) {
  $img = 'c://wamp/www/www.mysite.com/uploads/images/cat1/image'.$count++.'.png';
    file_put_contents($img, file_get_contents($image));
   }
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Thanks. Is creating a sequence of images correctly, but only the first image creates correctly. Other images are empty and php appears error for all images: Warning: file_get_contents(`https://urlimage`): failed to open stream: Invalid argument in C:\wamp\www\www.www.mysite.com\site\home.php on line 59 – Gislef Jan 04 '16 at 20:00
  • @Zeli have you tried opening the problem urls in a browser? Do they load OK? – Steve Jan 04 '16 at 20:09
  • yes the images normally carry when opened in the browser – Gislef Jan 04 '16 at 20:14
  • Hard to help without seeing the actual urls. Maybe there are additional spaces after the line breaks? If so try `file_get_contents(trim($image))` – Steve Jan 04 '16 at 20:33
  • My guess is the linebreaks where actually \r\n . – VolkerK Jan 04 '16 at 21:51
  • @VolkerK Are yes, that could be it. Imposable to know for sure without seeing the actual file contents – Steve Jan 04 '16 at 22:15
1

Your code

file_put_contents($img, $image);

will put the contents of $image, i.e. url, into the file, not the contents "behind" the url.
But instead of a string you can pass a stream resource as the parameter $data to file_put_contents. So open the stream via fopen/http-wrapper, check the result and pass it to file_put_contents.

$url = explode('\n', $urls);
foreach($url as $count=>$image) {
    $img = 'c://wamp/www/www.mysite.com/uploads/images/cat1/image'.$count.'.png';
    $fp = fopen($image, 'rb');
    if ( !$fp ) {
        yourErrorHandler();
    }
    else {
        $result = file_put_contents($img, $fp);
        // check $result here....
    }
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Thanks, It only saved the first image the others are not created and appears php errors. `Warning: fopen( `https://urlimage`): failed to open stream: Invalid argument in C:\wamp\www\www.www.mysite.com\site\home.php on line 60` and `Fatal error: Call to undefined function yourErrorHandler() in C:\wamp\www\www.mysite.com\site\home.php on line 62` – Gislef Jan 04 '16 at 20:16