PHP - file_put_contents 只保存一半的文件?

发布于 2024-10-05 21:52:24 字数 531 浏览 1 评论 0原文

我不知道发生了什么事!每次执行可以保存的文件数量是否有限制?

所以,我有一个数组,其中包含另一台服务器上图像的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半山落雨半山空 2024-10-12 21:52:24

某些条目的 $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

寂寞清仓 2024-10-12 21:52:24

您尝试从 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.

渡你暖光 2024-10-12 21:52:24

您可能需要编辑 /etc/php5/cgi/php.ini 中的 php.ini 文件(可能位于 /etc/php[x]/xxx 中的不同位置)并进行以下设置:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 16M      ; Maximum amount of memory a script may consume (16MB)

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:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 16M      ; Maximum amount of memory a script may consume (16MB)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文