File Upload Error
PHP failed to process a file upload due to size limits, permissions, or configuration issues.
Explanation
File upload errors occur when PHP cannot process uploaded files. The error code is stored in $_FILES['file']['error'] and can be one of several constants: UPLOAD_ERR_INI_SIZE (exceeds upload_max_filesize), UPLOAD_ERR_FORM_SIZE (exceeds MAX_FILE_SIZE), UPLOAD_ERR_PARTIAL (partially uploaded), UPLOAD_ERR_NO_FILE (no file uploaded). Laravel's uploaded file handling translates these into validation exceptions.
Common Causes
- File exceeds upload_max_filesize limit
- post_max_size too small
- Upload destination not writable
- Disk space exhausted
- Temporary directory missing
Solution
Check upload_max_filesize and post_max_size in php.ini and ensure they're large enough for your needs. Verify the upload_tmp_dir exists and is writable. Check file permissions on the upload destination directory. For Laravel, configure validation rules like max:10240 (in KB) or mimes:jpg,png. Use Laravel's store() method which handles temporary files properly. For large files, consider using chunked uploads or direct-to-S3 uploads.
Code Example
// Laravel validation for file uploads
$request->validate([
'file' => 'required|file|max:10240|mimes:pdf,doc,docx',
]);
// Store the file
$path = $request->file('file')->store('uploads');
// Store with custom name
$path = $request->file('file')->storeAs('uploads', 'document.pdf');
// Check upload errors manually
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
$errorMessages = [
UPLOAD_ERR_INI_SIZE => 'File exceeds server size limit',
UPLOAD_ERR_FORM_SIZE => 'File exceeds form size limit',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded',
UPLOAD_ERR_NO_FILE => 'No file was uploaded',
];
die($errorMessages[$_FILES['file']['error']]);
}
// php.ini settings
// upload_max_filesize = 64M
// post_max_size = 128M
Error Information
Language
php
Difficulty
Intermediate
Views
17
Related Errors
DevOps
Laravel Schedule Task Failure
A scheduled task failed to execute or completed with errors.
PHP
Include/Require File Not Found
The code tries to include or require a file that does not exist at the specified path.
PHP
Cannot Use String Offset as Array
The code tries to access a string using array syntax, treating it as an array.
PHP
Undefined Variable
The code references a variable that has not been declared or assigned a value.