Forum Moderators: coopster

Message Too Old, No Replies

Download PDFs automatically with PHP

         

andrewsmd

5:36 pm on Dec 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to download a pdf automatically and save it with php does anyone know how to do this? Let's say I have a link like <a href="www.someplace.com/somefile.pdf">Some file</a>
I need to download that pdf link and save it on my machine locally. Any ideas? Thanks,

andrewsmd

8:41 pm on Dec 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok so I solved the problem myself, and here is the script in case anyone else wants to use it. $urls is an array of all of the links I needed to download each pdf from
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
$g="837 Downloads\\".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);