PHP File Upload Not Working? Causes and Fixes (Empty $_FILES, Size Limits, Permissions)
Quick answer: Most upload failures in PHP come from size limits (upload_max_filesize, post_max_size, or the web server's own limit), a form without enctype="multipart/form-data", a destination folder PHP can't write to, or a wrong path in move_uploaded_file(). Check $_FILES['file']['error']: its code tells you exactly which one. If $_FILES and $_POST are both empty, the request was bigger than post_max_size.
Uploads fail silently surprisingly often: the form submits, the page reloads, and the file is nowhere. This guide goes through the causes in the order you should check them, from the form to the folder on disk.
Step 1: check the form
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
method="post": files can't be sent with GET;enctype="multipart/form-data": without it, only the file name is sent, and$_FILESstays empty;- the
nameof the input (file) must match what PHP reads in$_FILES['file'].
With JavaScript, send a FormData object and don't set the Content-Type header yourself: the browser adds the multipart boundary.
Step 2: read the error code
<?php
var_dump($_FILES); // temporary, while debugging
| Code | Constant | Meaning | Fix |
|---|---|---|---|
| 0 | UPLOAD_ERR_OK | Upload worked | The problem is later (moving the file) |
| 1 | UPLOAD_ERR_INI_SIZE | Larger than upload_max_filesize | Raise the limit (step 3) |
| 2 | UPLOAD_ERR_FORM_SIZE | Larger than the form's MAX_FILE_SIZE | Raise or remove that hidden field |
| 3 | UPLOAD_ERR_PARTIAL | Only part of the file arrived | Connection dropped or timed out; retry, check timeouts |
| 4 | UPLOAD_ERR_NO_FILE | No file selected | Validate on the client and server |
| 6 | UPLOAD_ERR_NO_TMP_DIR | No temporary folder | Set upload_tmp_dir to a folder that exists |
| 7 | UPLOAD_ERR_CANT_WRITE | Can't write to the temporary folder | Disk full or permissions on the temp folder |
| 8 | UPLOAD_ERR_EXTENSION | A PHP extension stopped the upload | Check the PHP error log |
$_FILES and $_POST are both empty
When the whole request is larger than post_max_size, PHP discards the body: no files, no form fields, and only a warning in the log ("POST Content-Length of ... bytes exceeds the limit"). Detect it like this:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($_POST) && empty($_FILES)
&& (int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > 0) {
exit('The file is too large for this server.');
}
Step 3: raise the size limits (all of them)
An upload must fit under every limit on the way. In php.ini (or the PHP-FPM pool, or .user.ini on shared hosting):
file_uploads = On
upload_max_filesize = 20M
post_max_size = 25M ; larger than upload_max_filesize: covers the other fields too
max_file_uploads = 20
max_input_time = 120 ; time allowed to receive the upload
Then the web server:
- Nginx:
client_max_body_size 25m;(the default is only 1 MB; larger uploads get 413 Request Entity Too Large before PHP even runs); - Apache:
LimitRequestBody, if set, must be large enough; - Cloudflare: the free plan limits request bodies to 100 MB.
Restart PHP-FPM or Apache after changing php.ini, and confirm the active values with phpinfo() in the browser (the command line can use a different php.ini). ini_set() doesn't work for these settings, because the upload is processed before your script runs.
Step 4: move the file to the right place
Uploaded files wait in a temporary folder and are deleted at the end of the request. Move them with move_uploaded_file(), using an absolute path:
<?php
$dir = __DIR__ . '/uploads';
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
error_log("Can't create $dir");
exit('Upload failed.');
}
$ext = 'jpg'; // derive it from the real file type, never from the original name
$name = bin2hex(random_bytes(16)) . '.' . $ext;
if (!move_uploaded_file($_FILES['file']['tmp_name'], "$dir/$name")) {
error_log('move_uploaded_file failed: ' . (error_get_last()['message'] ?? ''));
exit('Upload failed.');
}
A relative path like 'uploads/' depends on the current directory, which changes with includes and cron jobs. __DIR__ avoids that. Never use the user's original file name: it can overwrite other files or contain dangerous characters.
Step 5: fix folder permissions
If move_uploaded_file() returns false with "failed to open stream: Permission denied" in the log, the web server user can't write to the folder:
# Debian / Ubuntu
sudo chown -R www-data:www-data /var/www/site/uploads
# RHEL / Amazon Linux
sudo chown -R apache:apache /var/www/site/uploads
sudo chmod 755 /var/www/site/uploads
On SELinux systems (RHEL, CentOS, Amazon Linux), the folder also needs a writable context:
sudo chcon -R -t httpd_sys_rw_content_t /var/www/site/uploads
Don't use 777. See fixing "Permission denied" on PHP folders.
Other causes
- Disk full:
df -hon the server, including the partition with/tmp; - open_basedir: if the temp or destination folder is outside the allowed paths, PHP refuses it; the log says "open_basedir restriction in effect";
- Timeouts on slow connections: raise
max_input_timeand the web server's request timeouts for large files; - Memory errors when processing images: see Allowed memory size exhausted;
- Multiple files: with
name="files[]",$_FILES['files']['name']is an array: loop over the indexes.
Upload works but is it safe?
Once the upload works, validate it: check the real file type with finfo, limit the size, generate the file name yourself and block PHP execution in the upload folder. The full secure version is in secure file uploads in a PHP API.
Checklist
- Form with
method="post"andenctype="multipart/form-data"; - Input name matches
$_FILES['...']; - Error code read from
$_FILES['file']['error']; upload_max_filesize,post_max_sizeand the web server limit large enough;- Absolute destination path, folder exists;
- Web server user can write to the folder (and SELinux allows it);
- Disk has free space;
- File validated and renamed before saving.
Conclusion
A PHP upload has several places to fail: the form, PHP's size limits, the web server's limit, the temporary folder and the destination folder. The error code in $_FILES, plus the PHP and web server logs, tells you which one in almost every case. Fix that point, then make the upload safe.
Frequently asked questions
Why is $_FILES empty?
The form is missing enctype="multipart/form-data", or the request exceeded post_max_size (then $_POST is empty too).
Why do large uploads return 413?
The web server rejected the request before PHP. On Nginx, raise client_max_body_size.
Can I raise upload_max_filesize with ini_set()?
No. It must be set in php.ini, the PHP-FPM pool, .user.ini or .htaccess (mod_php only).
Why does move_uploaded_file() return false?
Usually the destination folder doesn't exist, the path is wrong, or the web server user can't write there. The PHP error log shows the reason.
Which permissions should the upload folder have?
Owned by the web server user, with 755. Avoid 777.