Jump to content

Problem with cURL & filesize()


whit3fir3

Recommended Posts

I have a function that uses cURL to download images and writes them to a file.  After the file is written I call fclose() on my file that I was writing to as well as curl_close() on the cURL object.  I want to get the size of the file by calling filesize();  When I do this it returns the value of FALSE.  The only thing that I can figure is that cURL still has some kind of lock on the file.  Can anyone thing of a way to clear to clear the lock and or a different way to get the size of the file?

 

Thanks,

 

whit3fir3

Link to comment
https://forums.phpfreaks.com/topic/183915-problem-with-curl-filesize/
Share on other sites

Any chance you are checking if the file exists first and that cached status is being returned later in your code? See this note from the filesize() section of the manual -

Note: The results of this function are cached. See clearstatcache() for more details.

Thank you for your quick response, but the clearstatcache() did not fix the problem.  You were correct though, I am checking to see if the file_exists() earlier in the code.  Any example of the code I am using is below.  I have tried clearstatcache() both with no parameters and with passing it the optional parameters of the file I want it to clear the stat cache for. 

 

      $file = fopen($localPath, 'a');
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$imgURL);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	curl_setopt($ch, CURLOPT_FILE,$file);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt(ch, CURLOPT_TIMEOUT, 20);
	curl_exec($ch);
	curl_close($ch);
	fclose($file);
	clearstatcache();
	$sizeOfFile = filesize($file);

The filesize function expects it's parameter to be a string containing the path to the file. In your script $file is a resource, not a string. Perhaps you instead want $sizeOfFile = filesize($localPath);

 

You could also get the information from cURL to see how big the downloaded file is.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.