Download Remote Files

Hello all,

I have a question regarding saving multiple files from a remote source. I want to use a php script to save all .jpg files in a dir of a remote place (my supplier in which I’ve asked and been granted permission to do so) but I have no real idea where to start actually.

I know how to grab a single file from their server using

[php]

<?php $url = 'theirurl/images/nameofimage.jpg'; $img = 'output.jpg'; file_put_contents($img, file_get_contents($url)); ?>

[/php]

Tho I am having trouble picturing in my head how I would make it go through the list of .jpg and simply grab the file name and save it as the same file name and then move on to the next file until all of the images are grabbed.

If this is possible I would really like to know at least how it’s possible.

Thanks

~
V

You would have to have a list of the files or get the directory listing from the server, then split up the directory listing and loop through it (or loop through the list).

I would start by getting the remote directory (rather than a file inside of it) and seeing what it returns. If the remote server does not allow directory listing, you will need to know the names of the files and input that into php, for example in an array:

[php]$files = array(‘pic1.jpg’, ‘pic2.jpg’);[/php]

It would then simply be a case of looping through that array and downloading each file:

[php]$site_path = ‘http://some-supplier.org/images/’;

foreach($files as $file) {

file_put_contents($file, file_get_contents($site_path . $file));

}[/php]

Could you post the view-source output of the directory in which the files are hosted? It might look something like this:

[code]

Index of /

Index of /

[ICO] Name Last modified Size Description

[DIR] computing/ 02-Dec-2011 10:48 -  
[DIR] helping/ 15-Dec-2011 18:07 -  
[DIR] hosting-panel/ 13-Dec-2011 11:06 -  
[DIR] jsherz/ 10-Dec-2011 22:00 -  
[DIR] old/ 15-Dec-2011 14:10 -  

[/code]
Sponsor our Newsletter | Privacy Policy | Terms of Service