在继续之前如何让php等待卷曲完成?

时间:2022-02-05 07:20:04

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().

第二次。我的猜测是,当执行filesize()时,curl仍在下载。

2 个解决方案

#1


2  

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

请注意,像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";

See www.php.net/clearstatcache

#2


0  

Well, I have the same problem. curl is supposed to be synchronous, but, depending on how you're using it, it's not synchronous.
If you call, after curl, a print, or echo, the content comes void. There's a strange delay. But I'll try this approach -

好吧,我有同样的问题。 curl应该是同步的,但是,根据你使用它的方式,它不是同步的。如果您在卷曲,打印或回声后调用,则内容无效。有一个奇怪的延迟。但我会尝试这种方法 -

print_r(curls_getinfo($CR));

Doing everything in one step may solve the problem.

一步完成所有事情可以解决问题。

#1


2  

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

请注意,像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";

See www.php.net/clearstatcache

#2


0  

Well, I have the same problem. curl is supposed to be synchronous, but, depending on how you're using it, it's not synchronous.
If you call, after curl, a print, or echo, the content comes void. There's a strange delay. But I'll try this approach -

好吧,我有同样的问题。 curl应该是同步的,但是,根据你使用它的方式,它不是同步的。如果您在卷曲,打印或回声后调用,则内容无效。有一个奇怪的延迟。但我会尝试这种方法 -

print_r(curls_getinfo($CR));

Doing everything in one step may solve the problem.

一步完成所有事情可以解决问题。