<?php
set_time_limit(0);
$url = 'http://www.some.url/file.pdf';
$path = 'files/file.pdf';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
This is the code that I use to download a particular pdf file froma given url. What if there a many files in that url by names file1.pdf, file2.pdf etc. How can I check while running a loop, when to end the loop as the files will be present up to a limited number ?
Please help!
Checking for 404 code:
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* file NOT found */
}
Checking mime type:
$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if($mimeType == 'application/pdf') {
/* It IS pdf file */
}
But note, that mime type can be other, but it'll be still PDF file! Also, check mime type by your PDF files: echo them to understand what u must look for. I'm not rly sure, that code in if statement is right (it is example only)
You can call curl_get_info() right after curl_exec().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With