Reputation: 1969
<?php
$source = 'http://www.xxx.com/1.jpg';
$fileBody = date('YmdHis') . rand(1000, 9999);
$extension = pathinfo($source, PATHINFO_EXTENSION);
$fileName = $fileBody . '.' . $extension;
$ch = curl_init($source);
$fp = fopen($path . $fileName, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
clearstatcache();
return $fileName;
This is how I grab image from internet, the image saved successfully and I will return file name for ajax to make immediately thumbnail, but sometimes php return the $fileName when it still processing download, therefore JavaScript reveal empty image on the page, how response after the file indeed been download.
Upvotes: 2
Views: 795
Reputation: 2505
$downComplete = false;
while(!$downComplete)
{
if(!file_exist($filePath))
{
sleep(1);
}
else
{
$downComplete = true;
break;
}
}
Hi,Above is an idea for check if file is completely downloaded,i think it's useful, main idea is check saved file all the time until its finished download,then you can display the img to front end..you can add it after your curl code,i didn't run it myself,so just an idea..
Upvotes: 1
Reputation: 7866
curl_exec
returns true
on success and false
on failure. Use that information.
Also, You can check curl_getinfo
to make sure the transfer completed successfully and was not empty. (You get http_code
there for example, as well as content_type
and size_download
).
Upvotes: 2