<?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()
.
pass image link inside file_get_contents();
then check using preg_match();
<?php $link = $image->img1;
$filecontent=file_get_contents($link);
if(preg_match("/^%PDF-1.5/", $filecontent)){
echo "Valid pdf";
}else{
echo "In Valid pdf";
}
?>
You can also check: to get last three character of your file
<?php
$img_type = substr($image->img1, -3); ?>
<?php if(preg_match("/^%PDF-1.5/", $filecontent) || $img_type == 'pdf' ){ }?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.