Laravel上传超4MB文件报错File does not exist at path {$path},求排查方向
Hey there! Let's dig into this issue you're facing. Based on what you've described—local server handles 7MB+ files fine, but your host throws "File does not exist at path" for files over ~4MB—this is almost certainly a host server configuration problem, not your code. Here's why and what to check:
核心判断依据
Your local environment works perfectly with large files, which means your Laravel validation rules, upload logic, and storage setup are all correct. The only variable here is the host's server environment, so that's where we should focus first.
最可能的主机端问题点
- PHP upload limits: Most shared hosts default
upload_max_filesizeandpost_max_sizeto small values (like 2MB or 4MB). Even if you setmax:20480in Laravel, PHP will reject the file before it ever reaches your application, leaving Laravel with no file to process (hence the "does not exist" error).- Quick check: Create a temporary route in your Laravel app that returns
phpinfo()output, then look for these two parameters to confirm their actual values.post_max_sizeneeds to be larger thanupload_max_filesize(since POST requests include other form data too).
- Quick check: Create a temporary route in your Laravel app that returns
- Web server restrictions: If your host uses Nginx, the
client_max_body_sizedirective also caps upload sizes. It often defaults to 4MB, and Nginx will block the request before it gets to PHP. Your host will need to adjust this in their Nginx config. - Temporary directory permissions: Less common, but if the host's
upload_tmp_dir(where PHP stores files temporarily during upload) doesn't have proper write permissions, large files might fail to save there, leading Laravel to think the file doesn't exist.
代码端的快速排查(可能性极低)
Just to rule out any edge cases:
- Double-check your validation rule: Make sure you're actually applying the
max:20480rule correctly (note: 20MB is 20480 KB—your20840might be a typo, but that's not the root issue here). For example:$request->validate([ 'your_file_field' => 'required|file|max:20480', ]); - Confirm storage directory permissions: Ensure the
storage/appdirectory (or whichever disk you're using) has write permissions on the host. But since small files work, this is unlikely to be the problem.
下一步建议
Reach out to your host's support team and ask them to:
- Verify and increase
upload_max_filesizeandpost_max_sizeto at least 20MB - Check if
client_max_body_size(for Nginx) or equivalent (for Apache) is set to a value larger than your max file size - Confirm the PHP temporary upload directory has proper write permissions
This should resolve the issue in almost all cases. If they insist their config is correct, then we can dive deeper into code, but based on your scenario, host config is the clear culprit.
内容的提问来源于stack exchange,提问作者dot321

