tags:

views:

1872

answers:

2

It seems if I do something like

$file = fopen($filepath, "w");
$CR = curl_init();
curl_setopt($CR, CURLOPT_URL, $source_path);
curl_setopt($CR, CURLOPT_POST, 1);
curl_setopt($CR, CURLOPT_FAILONERROR, true);
curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CR, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($CR, CURLOPT_FILE, $file);
$result = curl_exec( $CR );
$error = curl_error( $CR );
print filesize($filepath);

I get a different result than if I just run

print filesize($filepath);

a second time. My guess is that curl is still downloading when do a filesize().

A: 

I don't have an answer, but perhaps we can get closer to deducing what's going on here. cURL is supposed to be synchronous, so it's strange that you get a different size the second time. I would expect the file to be complete before curl_exec returns. Try calling this function immediately following your call to curl_exec:

print_r(curl_getinfo($CR));

Pay attention particularly to CURLINFO_SIZE_DOWNLOAD. Perhaps the OS is doing something funny with the file? What OS are you using, and is the file being saved locally or via a network-based connection?

Ethan T
+1  A: 

Note that functions like filesize() cache their result, try adding a call to clearstatcache() above 'print filesize(...);'. Here is an example:

$file = '/tmp/test12345';
file_put_contents($file, 'hello');
echo filesize($file), "\n";
file_put_contents($file, 'hello world, this is a test');
echo filesize($file), "\n";
clearstatcache();
echo filesize($file), "\n";

See www.php.net/clearstatcache

too much php