快速调试和有效修复大图像文件上传失败的最佳方法

发布于 2024-09-28 16:44:27 字数 1591 浏览 6 评论 0原文

考虑一个普通的 PHP 图像上传功能(不使用 AJAX),偶尔会出现大图像上传失败的问题 - 在一台测试服务器上的频率较低,而在另一台测试服务器上的频率较高。假设调试器尚未开始调试问题并且不存在文件/文件夹权限问题,如何继续?

我确定我已打开 file_uploads。我不想只是盲目地设置一些安全值或增加值直到它起作用。基本上,我希望这些值与我相关的模块完全相同。如果这是最好的方法,我准备覆盖相关模块中的设置。

根据文件上传相关的设置,这些都是相关/相关的设置 -
* file_uploads
* upload_max_filesize
* 最大输入时间
* 内存限制
* max_execution_time
* post_max_size

  1. 查找相关脚本的参数/值 -
    为了找出其中哪一个/有多少个实际上被我的脚本违反并导致失败,我需要首先找到我的脚本的相应值。如何为我的脚本查找以下值:

    • 上传文件的总大小
    • 输入时间
    • 内存使用情况
    • 脚本执行时间
    • 发布的数据大小

    哪些工具可以用于相同的目的。使用 PHP 代码,我想,我可以找出一些:

    • 脚本执行时间 - 脚本开始和结束时 microtime(true) 之间的差异。
    • 上传的文件总大小 - 在 $_FILES 上进行 Foreach 循环以查找 ['size'] 属性的总和

    如何找出其他信息,例如内存使用情况、输入时间等?

  2. 在何处/如何覆盖
    最后,当我发现违规设置时,假设我需要增加/覆盖其中 2 个设置的值。在哪里应用覆盖?我猜想为 htaccess 或 PHP 脚本中的所有模块设置 memory_limit 等是不正确的。相反,仅在相关模块中应用会更好。我说得对吗?

  3. 要求较低的模块的设置
    另外,对于其他不需要太多资源的模块,在仔细研究模块的资源需求后,覆盖设置以减少资源是否明智?会减少不必要的资源消耗吗?如果是这样,是否拥有这些设置的 2 或 3 种组合(根据项目要求,将它们命名为普通脚本、重型文件上传)并调用单个函数来为每个模块加载任意一种组合?

  4. 内存限制注意事项
    关于 memory_limit 这里提到 -

    <块引用>

    设置太高的值可能非常危险,因为如果同时处理多个上传,所有可用内存都将被用完,并且消耗大量内存的其他不相关脚本也可能会影响整个服务器。

对此应采取哪些一般预防措施?

谢谢,
桑迪潘

Consider a normal PHP image upload functionality (not using AJAX) and there occurs this problem of large image upload failing occasionally - less frequently on one test server and more frequently on another test server. Assuming the debugger has not yet started debugging the problem and there are no file/folder permission issues, how to proceed?

I am sure I have file_uploads on. I do not want to just blindly set some safe values or increase the values until it works. Basically, I want the values to be exactly as per my concerned modules. I am ready to override the settings in my concerned modules, if that is the best approach.

According to settings related to file upload, these are all the relevant/related settings -
* file_uploads
* upload_max_filesize
* max_input_time
* memory_limit
* max_execution_time
* post_max_size

  1. Finding parameters/values for concerned script -
    So that I can find out which one of them is/how many of them are actually being violated by my script and causing the failure, I need to first find the corresponding values for my script. How to find the following values for my script:

    • Total uploaded files size
    • Input time
    • Memory usage
    • Script execution time
    • Posted data size

    Which tool(s) can be used for the same. Using PHP code, I think, I can find out a few:

    • Script execution time - Difference between microtime(true) at script start and end.
    • Total Uploaded file size - Foreach loop on $_FILES to find the sum of ['size'] attribute

    How to find out the rest like Memory Usage, Input time etc.?

  2. Where/How to override
    Finally, when I have found the violating setting(s), suppose I need to increase/override values for 2 of the settings. Where to apply the override? I guess it is not correct to set memory_limit etc. for all the modules in htaccess or in PHP script. Rather, applying only in the concerned module is better. Am I correct?

  3. Settings for Less demanding modules
    Also, for other modules, where much resources are not needed, is it good/wise to override the settings to reduce them, after carefully studying the resource requirements of the modules? Will it reduce unnecessary resource consumption? If so, how about having 2 or 3 combinations of these settings (depending on the project requirements, naming them normal-script, heavy-file-upload) and calling a single function to load any one combination for every module?

  4. memory_limit precautions
    Regarding memory_limit it is mentioned here that -

    Setting too high a value can be very dangerous because if several uploads are being handled concurrently all available memory will be used up and other unrelated scripts that consume a lot of memory might effect the whole server as well.

What general precautions to take about this?

Thanks,
Sandeepan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

演多会厌 2024-10-05 16:44:27

调试的一些想法:

对于手动测试,我会准备一系列不同尺寸的图像,其净尺寸(宽度x高度)以小步增加:100 x 100、100 x 200、100 x 300 ....并尝试他们。在某些时候,如果问题是内存限制,它们可能会开始失败。您可以仅为自己打开error_reporting()(也许使用某种调试cookie),这样您就可以看到到底是什么失败了。

如果这不是一个选项,我会设置某种用于长期日志记录的机制,在开始调整大小之前将图像的尺寸以及 $_FILES 的内容存储到日志文件或表中> 数组。在脚本成功结束时,向该条目添加“确定”。这样,您将能够找到有关失败上传的更多信息,如果它们成功到达脚本(并且不会由于超时设置而提前失败)。

此外,对于不需要太多资源的其他模块,覆盖设置以减少资源是好的/明智的

我认为答案始终是“否”。据我所知,内存限制是可以分配的最大内存限制,但并不是为每个请求保留该数量。我从未听说过有人以这种方式微调内存限制。

但是,如果系统的某些部分(例如图像缩放器)需要极高的内存限制,则仅对它们应用特定的 memory_limit 设置可能是明智的,例如通过 .htaccess 设置

A few ideas for debugging:

For manual testing, I would prepare a series of images with different dimensions whose net size (width x height) increases in small steps: 100 x 100, 100 x 200, 100 x 300 .... and try them. At some point, they could start failing if the problem is the memory limit. You could turn on error_reporting() for yourself only (maybe using a debugging cookie of some sort) so you see what exactly fails.

If that's not an option, I would set up a mechanism of some sort for long-term logging that stores the image's dimensions into a log file or table before the resizing starts, and also the contents of the $_FILES array. At the successful end of the script, add an "OK" to that entry. That way, you will be able to find out more about the failed uploads, if they make it through to the script (and don't fail beforehand due to a timeout setting).

Also, for other modules, where much resources are not needed, is it good/wise to override the settings to reduce them

I think the answer is always "no". As far as I know, the memory limit is the maximum limit of memory that can be allocated, but that amount is not reserved for every request. I have never heard of anybody fine-tuning the memory limit in this way.

However, if some parts of the system (e.g. the image resizer) require an enormously high memory limit, it may be wise to apply specific memory_limit settings only to them, e.g. through a .htaccess setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文