PHP - file_put_contents 只保存一半的文件?
我不知道发生了什么事!每次执行可以保存的文件数量是否有限制?
所以,我有一个数组,其中包含另一台服务器上图像的 URL 列表。它按预期工作,并且返回有效的 URL。问题是当尝试保存它们时,当返回 100 个 URL 时,只保存了 52 个文件。正如你所看到的,我在保存图像之前回显了图像路径,并且在计算行数之后,它总计为 100。(不,当然我没有手动计算它们!)
我正在运行一个循环:
for ($i=0; $i <= $count; ++$i) {
$url = $response['images'][$i]['image'];
$path = 'exported/'.$response['images'][$i]['date'].'.jpg';
echo $response['images'][$i]['image'].'<br />';
file_put_contents($path, file_get_contents($url));
}
任何想法是什么发生什么事了? 非常感谢!
I have no idea what's going on! Is there a limit on how many files can be saved per execution?
So, i have an array with a list of URLs to images on another server. That works as it should, and it returns valid URLs. The problem is when try to save them, only 52 files are saved when there are 100 URLs returned. As you see i echo out the imagepath before i save the image, and it after counting the lines it adds up to 100. (no, of course i didn't count them manually!)
I'm running a loop:
for ($i=0; $i <= $count; ++$i) {
$url = $response['images'][$i]['image'];
$path = 'exported/'.$response['images'][$i]['date'].'.jpg';
echo $response['images'][$i]['image'].'<br />';
file_put_contents($path, file_get_contents($url));
}
Any ideas what's going on?
Thanks a bunch!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
某些条目的
$response['images'][$i]['date']
值是否相同?您可能会生成相同的输出文件名 ($path
) 并让文件相互覆盖。您可以尝试回显$response['images'][$i]['date']
以确保Do some of the entries have the same value for
$response['images'][$i]['date']
? You might be generating the same output filename ($path
) and have files overwriting each other. You can try echoing$response['images'][$i]['date']
to make sure您尝试从 PHP 下载 100 个图像时可能会遇到脚本超时。作为一种脚本语言,PHP 的运行时间应该只有 1-2000 毫秒。然而,我相信基本的 PHP 安装将脚本执行时间设置为惊人的 30 秒,这对于您的脚本来说可能不够长。
You are probably reaching the script timeout trying to download 100 images from PHP. As a scripting language, PHP is only supposed to run for 1-2000ms. However, basic PHP installs have script execution set at a whooping 30 seconds I believe, which probably isn't long enough for your script.
您可能需要编辑 /etc/php5/cgi/php.ini 中的 php.ini 文件(可能位于 /etc/php[x]/xxx 中的不同位置)并进行以下设置:
You may want to edit your php.ini file in /etc/php5/cgi/php.ini (may be in a different location in /etc/php[x]/xxx) and up the following settings: