PHP Warning: move_uploaded_file() unable to move

PHP Warning: move_uploaded_file() unable to move

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The move_uploaded_file() Permission Trap

When debugging a file upload failure, seeing a generic error message like "There was an error uploading the file" is frustrating because it hides the root cause. You need to look at the Apache error log. In this specific case, the log reveals the exact discrepancy between where PHP thinks the file is and where your script wants to put it:

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpVLOnn3' to '/var/www/media2net/uploads/downloaded_file.png'

The error isn't about missing permissions on the destination directory or safe mode restrictions. The issue is a fundamental mismatch in the file path construction logic within your PHP code.

The Logic Flaw in Path Construction

Your code attempts to append the original filename to the target path:

$target_path = "/var/www/media2net/uploads/uploads"; // Base directory
// ... later ...
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);

This creates a variable $target_path that resolves to something like /var/www/media2net/uploads/uploads/filename.png. However, the error log explicitly shows PHP trying to move the file to /var/www/media2net/uploads/downloaded_file.png.

Where did downloaded_file.png come from? It appears your Android application is sending a request with a specific filename header, but there is a subtle disconnect between how you are constructing the path and what the error log reports. More critically, you are overwriting $target_path twice in the snippet provided. The first line sets it to /var/www/media2net/uploads/uploads. If the variable isn't being used as intended, or if there is a caching issue with the variable state before the append operation, the logic breaks.

However, looking closer at the error log path vs your code:

  • Error Log Path: /var/www/media2net/uploads/downloaded_file.png
  • Your Code Path: /var/www/media2net/uploads/uploads/filename.extension

These two paths do not match. The move_uploaded_file() function requires the source (the temporary file in /tmp) and the destination to be on the same filesystem or for the destination to be directly writable by the web server user. If your code constructs a path that includes the subdirectory uploads/, but your error log shows a path missing that subdirectory, the variable $target_path is not holding the value you expect at the moment of execution.

The Real Culprit: Directory Traversal and Permissions

Even if you fix the string concatenation to match exactly what you intend (e.g., ensuring both paths include /uploads/uploads/), you are running into a security and permission trap.

You are attempting to move a file from /tmp (owned by root) to your web directory. The command move_uploaded_file() uses the underlying OS rename() or mv system calls. If the destination path does not exist, PHP will try to create it. However, if you are reusing a directory name that might already contain files, or if you are trying to move into a directory where the web server user (usually www-data, apache, or _http) lacks write permissions for that specific subdirectory, the move will fail.

The error log confirms the move failed. The most common reason for this failure in production environments is that the Apache user cannot write to the destination directory, or the directory structure is slightly different than what your PHP string concatenation assumes.

Correcting the Implementation

To resolve this, you must ensure the variable $target_path is constructed precisely and validated before moving. Here is a robust pattern for handling uploads:

  1. Define a base directory that exists and is writable by the web server.
  2. Sanitize the filename to prevent directory traversal attacks (e.g., blocking ../../).
  3. Ensure the target path does not include the subdirectory prefix if you aren't intending to create it dynamically, or ensure the full path including uploads/ matches your actual directory structure.
$uploadDir = '/var/www/media2net/uploads'; // Base writable directory

// Sanitize filename to prevent directory traversal
$fileName = basename($_FILES['uploadedfile']['name']);
$targetPath = $uploadDir . '/' . $fileName;

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $targetPath)) {
    echo "The file {$fileName} has been uploaded";
    
    // Set permissions carefully. 755 allows execute, which might be needed for directories,
    // but usually files should be 644 (readable by all, writable by owner).
    // chmod("{$uploadDir}/" . $fileName, 0755); 
} else {
    echo "There was an error uploading the file.";
}

Why chmod Might Be Causing Issues

In your original snippet, you executed chmod("uploads/" . basename(...), 755) immediately after a successful move. This is dangerous for two reasons:

  1. Path Ambiguity: You are passing a relative path (uploads/...) to chmod instead of an absolute path. If the script runs from different working directories, this fails or affects unintended files.
  2. File Permissions vs. Directory Permissions: Setting a file permission to 755 (rwxr-xr-x) gives every user read and execute permissions but no write permissions. While this is often correct for static assets, it can be confusing. If the script intended to allow further modification, this would block it. More importantly, if the move failed earlier due to permission issues on the destination folder, running chmod afterward does nothing because the file never moved there.

Conclusion

The error move_uploaded_file() unable to move is almost exclusively a filesystem or path logic issue, not a PHP function bug. By aligning your constructed $target_path with the actual directory structure and ensuring the web server user has write access to that specific location, you can resolve the conflict between the source temp file and the destination. Always use absolute paths in your scripts to avoid ambiguity regarding the current working directory.

Our network