You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linode服务器图片上传报PHP内存耗尽错误求助

Memory Exhaustion Error When Uploading Images to Linode (Laravel API)

Hey there, let's figure out why your image upload API is hitting that memory limit on Linode, even after you've adjusted PHP settings. The error pointing to Monolog's LineFormatter is a key clue—it's not just the upload itself eating up memory, but likely the logging process trying to handle large data related to the upload.

Possible Causes & Fixes

1. You might be editing the wrong php.ini file

Linode servers (especially those running PHP-FPM alongside Nginx) often have separate php.ini files for CLI and web requests. If you modified the CLI version, your web server won't pick up those changes.

  • Check which php.ini your web server uses:
    Add a temporary controller method or route to output phpinfo();, then look for the "Loaded Configuration File" line. That's the file you need to edit.
  • Restart your web service after changes:
    After updating memory_limit, post_max_size, and upload_max_filesize, restart PHP-FPM and your web server to apply changes. For example:
    sudo systemctl restart php8.2-fpm nginx
    
    (Replace php8.2-fpm with your actual PHP version.)

2. Stop loading the entire image into memory at once

Your current code uses File::get($image) which reads the entire file into RAM—this is a major problem for large images. Instead, use Laravel's built-in streaming methods to upload the file in chunks:

Replace this line:

$upload=Storage::disk('local')->put($url, File::get($image));

With either of these options:

  • Using putFileAs (simplest):

    $upload = Storage::disk('local')->putFileAs('', $image, $url);
    

    Laravel handles streaming the file directly from the temp path to storage without loading it all into memory.

  • Manual streaming (more control):

    $stream = fopen($image->getRealPath(), 'r');
    $upload = Storage::disk('local')->put($url, $stream);
    fclose($stream);
    

This will drastically reduce memory usage during the upload.

3. Check your Monolog logging configuration

The error occurs in LineFormatter, which suggests Monolog is trying to log a huge amount of data (like the raw image content from the request). This can happen if your logger is configured to log full request details, including file uploads.

  • Update your logging config:
    Open config/logging.php and ensure your log channels aren't capturing unnecessary request data. For example, if you're using the stack or single channel, check if any processors are logging the entire $request object—exclude file inputs from being logged.

  • Quick fix:
    Before logging (or right after validation), you can unset the file from the request to prevent it from being included in logs:

    $request->request->remove('image');
    

4. Verify your Linode instance's available memory

Even if you set memory_limit=-1, your server's physical memory might be too small. If you're on a low-memory Linode plan (like 1GB), the combination of your Laravel app, web server, and other processes could be using up all available RAM when handling large uploads.

  • Check server memory usage:
    Run this command in your Linode terminal:
    free -h
    
    If you're consistently using most of the available memory, consider upgrading your Linode instance or optimizing other services to free up RAM.

Summary

Start with fixing the file upload method to avoid loading the entire image into memory—that's the most immediate fix. Then double-check that your PHP config changes are actually being applied by the web server. Finally, rule out logging and server memory constraints as contributing factors.

内容的提问来源于stack exchange,提问作者warihana gumah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:58:52