Jump to content

Default How To download multiple files simultaneously with php


Recommended Posts

Hi

 

i need a simple php code but i can't seem to figure it out since i am not a php expert

 

i have a list of image url in a text file (say image.txt).

 

i want to run a php script to get all the image from that url file and save it to images folder on my server.

 

have been searching for hours but just could not make this to work.

 

any help would be much appreciated.

 

thank you.

well you can use file_get_contents / file_put_contents or fopen

but php will not do this simultaneously you have to download each image one at a time

you can thread php but only in a CLI environment not when it is being used as a web server.

 

Scott.

I have found this code:

 

--------

<?php

 

$urls=array(

'http://f.askapache.com/mp3/12-lessons-for-those-afraid-of-css.mp3',

'http://f.askapache.com/mp3/27-request-methods-for-use-with-apache-and-rewritecond-and-htaccess.mp3',

'http://f.askapache.com/mp3/301-redirect-with-mod_rewrite-or-redirectmatch.mp3',

'http://f.askapache.com/mp3/404-errorpages.mp3',

'http://f.askapache.com/mp3/503-service-temporarily-unavailable.mp3',

'http://f.askapache.com/mp3/adsense-robots.mp3',

'http://f.askapache.com/mp3/alexa-toolbar-firefox.mp3',

'http://f.askapache.com/mp3/allowing-access-from-1-static-ip-and-deny-the-rest.mp3',

'http://f.askapache.com/mp3/apache-authentication-in-htaccess.mp3');

 

$save_to='/home/user/htdocs/mp3/';

 

$mh = curl_multi_init();

foreach ($urls as $i => $url) {

    $g=$save_to.basename($url);

    if(!is_file($g)){

        $conn[$i]=curl_init($url);

        $fp[$i]=fopen ($g, "w");

        curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);

        curl_setopt ($conn[$i], CURLOPT_HEADER ,0);

        curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);

        curl_multi_add_handle ($mh,$conn[$i]);

    }

}

do {

    $n=curl_multi_exec($mh,$active);

}

while ($active);

foreach ($urls as $i => $url) {

    curl_multi_remove_handle($mh,$conn[$i]);

    curl_close($conn[$i]);

    fclose ($fp[$i]);

}

curl_multi_close($mh);

?>

----

 

my problem is let say i have in a text file a list of image url

 

for example

 

http://domain.com/image/image1.jpg

http://domain.com/image/image2.jpg

http://domain.com/image/image3.jpg

http://domain.com/image/image4.jpg

http://domain.com/image/image5.jpg

 

and i have thousand of it

 

how do i use the code above to easily download all the images into my image folder.

 

note that i have to insert '  in the beginning and at the end and then a ,

 

so, it is difficult for me to do it in excel as it will treat , as ","

 

any help will be much appreciated.

 

thank you.

 

 

thanks for the help.. ie to use file()

 

but unfortunately, i cannot insert it into the code above since it will return the error

 

Warning: curl_multi_remove_handle() expects parameter 2 to be resource, null given in down-image.php on line 24

 

Warning: curl_close(): supplied argument is not a valid cURL handle resource in down-image.php on line 25

 

Warning: fclose(): supplied argument is not a valid stream resource in down-image.php on line 26

 

probably because the array return an array but without the , at the end of each line...

 

anyone can help me on this matter.

 

any help would be much appreciated.

 

thank you.

 

 

Just use WGET from your command line. Easy:

 

cd to images folder:

cd /var/www/html/mywebsite.com/images

 

get images from the urls in the text file

wget -i /path/to/images.txt

 

Done

 

Thanks Neil. It works.

 

i make some modification since i want it to run as cron job, so i put it in a php file and execute it

 

the code is like this

 

<?php

 

exec("wget -i /path/to/images.txt");

 

?>

 

remember to put the php file in the folder that you want to store the images...

 

thank you every one for the help

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.