There is an xls document which links to images (2000 lines) to a third-party resource. enter image description here

I copied all the links and put in a notebook and put it on the server.

$imageUrl = './bb.txt'; $imageFilePath = './img/'.foto.'.jpg'; // вопрос тут.как ставить оригинальное имя изображения? file_put_contents($imageFilePath, file_get_contents($imageUrl)); 

If I put the name of the photo that will be assigned to it for example foto, then it downloads only 1 photo. I need a crowd.

randomized the name

  $ran= substr(uniqid('', true), -5); $imageUrl = './bb.txt'; $imageFilePath = './img/'.$ran.'.jpg'; file_put_contents($imageFilePath, file_get_contents($imageUrl)); 

still only downloads 1 photo. example from where to download files here badja94f.beget.tech/bb.txt

  • one
    Create an array (list with photo names) and process it in a loop. - Edward
  • one
    Ask the names of the counter, what's the problem - Dmitriy
  • I am not strong in php. I need original names - Andrey Vladimirovich
  • did $ran= substr(uniqid('', true), -5); $imageUrl = './bb.txt'; $imageFilePath = './img/'.$ran.'.jpg'; file_put_contents($imageFilePath, file_get_contents($imageUrl)); $ran= substr(uniqid('', true), -5); $imageUrl = './bb.txt'; $imageFilePath = './img/'.$ran.'.jpg'; file_put_contents($imageFilePath, file_get_contents($imageUrl)); still only downloads 1 photo. example here badja94f.beget.tech/bb.txt - Andrey Vladimirovich
  • one
    Additional information needs to be added to the question. Nobody will read the code in the comments, because it is unreadable. - Enikeyschik

1 answer 1

First, collect the links to the photo into an array:

 $photos = [ 'https://partners.eksmo.ru/images/ishopcover/ITD000000000317230_cover1.jpg', 'https://partners.eksmo.ru/images/ishopcover/ITD000000000242165_cover1.jpg', 'https://partners.eksmo.ru/images/ishopcover/ITD000000000323557_cover1.jpg', 'https://partners.eksmo.ru/images/ishopcover/ITD000000000291758_cover1.jpg', 'https://partners.eksmo.ru/images/ishopcover/ITD000000000480406_cover1.jpg', 'https://partners.eksmo.ru/images/ishopcover/ITD000000000163903_cover1.jpg' ]; foreach ($photos as $i => $photo) { $imageFilePath = './img/photo_'. ++$i .'.jpg'; file_put_contents($imageFilePath, file_get_contents($photo)); } 

At the end of the script, images with the names photo_1.jpg , photo_2.jpg , photo_3.jpg , etc. will appear in the img folder.


UPD: In view of clarifications from comments:

 $url = 'http://badja94f.beget.tech/bb.txt'; $photos = explode(PHP_EOL, file_get_contents($url)); foreach ($photos as $photo) { $fname = './img' . substr($photo, strripos($photo, '/')); file_put_contents($fname, file_get_contents($photo)); } 
  • downloads only 3 images, while everyone has a downloaded size of 0 bytes and their names are not original - Andrey Vladimirovich
  • here from here you need on the links to download badja94f.beget.tech/bb.txt - Andrey Vladimirovich
  • No, I want to download images from this file using the links that are listed there - Andrey Vladimirovich
  • + with the original image name that they have in these links - Andrey Vladimirovich
  • Edward, thank you, it worked. I just have 2000-3000 thousands of images in the file. I thought that the links should be read from a notebook. And how to make the names of the images the same as in the file, for example ITD000000000317230_cover1 - Andrey Vladimirovich