azlan Posted March 6, 2009 Share Posted March 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/ Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-777959 Share on other sites More sharing options...
azlan Posted March 6, 2009 Author Share Posted March 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-777962 Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 if you have each URL on a new line you could use $urls = file("images.txt"); file() opens a file and creates a array each array element is a new line Scott. Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-777974 Share on other sites More sharing options...
azlan Posted March 6, 2009 Author Share Posted March 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-778252 Share on other sites More sharing options...
JonnoTheDev Posted March 6, 2009 Share Posted March 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-778278 Share on other sites More sharing options...
azlan Posted March 6, 2009 Author Share Posted March 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148205-default-how-to-download-multiple-files-simultaneously-with-php/#findComment-778287 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.