1

I'm trying to download all the images from an array and store them on my server using PHP.

This is my PHP code:

$IMAGES = 'http://url.com/image.jpg, http://url.com/image2.jpg, http://url.com/image-test.jpg, http://url.com/image6.jpg, http://url.com/image.jpg';

$images = array(''.$IMAGES.''); 

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

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

    $name0 = implode(" ",$name);

    copy(''.$name0.'', '/test/'.$name0.'.jpg');     


}

When I run my code, I don't get any images stored on my server and I get a warning message on my php page.

Could someone please advise on this issue?

The warning message I get is this:

Warning: copy(/test/http:/url.com/image.jpg http:/url.com/image2.jpg in line 88

and this is on line 88:

copy(''.$name0.'', '/test/'.$name0.'.jpg'); 
2
  • What's the warning message? And why do you use array like that? You save images paths as a single string in the database or what? Commented Jul 28, 2016 at 9:05
  • The url passed to the copy seems wrong, define images as an array: $IMAGES = array('url.com/image.jpg', 'url.com/image2.jpg', 'url.com/image-test.jpg', 'url.com/image6.jpg', 'url.com/image.jpg'); P.S Dont mix variables like that, you might have a bad time... Commented Jul 28, 2016 at 9:07

2 Answers 2

2

Try the following:

$IMAGES = 'http://url.com/image.jpg, http://url.com/image2.jpg, http://url.com/image-test.jpg, http://url.com/image6.jpg, http://url.com/image.jpg';

$images = explode(', ',$IMAGES); 

foreach($images as $image) {

   $name = basename($image);
   $newfile = $_SERVER['DOCUMENT_ROOT'] .'/test/'.$name;

   if(copy($image, $newfile)){
      echo 'Successfully downloaded '. $image;
   }else{
      echo 'Download failed for '. $image;
   }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Your array is containing 1 list for it to work you have to do something like this

$images= array(
  'http://url.com/image.jpg', 
  'http://url.com/image2.jpg',
  'http://url.com/image-test.jpg',
  'http://url.com/image6.jpg', 
  'http://url.com/image.jpg'
);

You can't make a string to a array the way you want - you can however use explode to "explode" the string to a array by doing something like this

$images = explode(',',$IMAGES);

you however get a problem with urls with a , in it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.