2008-11-06 119 views
1

看來,如果我這樣做如何讓php等待curl在繼續之前完成?

$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); 

我得到不同的結果,如果我只是運行

print filesize($filepath); 

第二次。我的猜測是curl仍然在下載文件大小()時進行下載。

回答

2

請注意,像filesize()這樣的函數會緩存它們的結果,請嘗試向'print filesize(...);'上方的clearstatcache()添加一個調用。這裏有一個例子:

$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"; 

見www.php.net/clearstatcache

0

嗯,我有同樣的問題。 curl應該是同步的,但是,取決於你如何使用它,它不是同步的。
如果您在捲曲,打印或回聲之後調用,則內容將失效。有一個奇怪的延遲。 但我會嘗試這種方法 -

print_r(curls_getinfo($CR)); 

這樣一個步驟都可以解決這個問題。

相關問題