Move_uploaded_file() function is not working

Move_uploaded_file() function is not working

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

Stefan Bogdanescu

Founder & Senior Architect

The Critical Difference Between tmp_name and name in PHP Upload Handlers

When debugging an upload script where move_uploaded_file() fails to execute, the root cause is almost invariably a misunderstanding of how PHP stores temporary file data versus its original filename. In the provided example, the code attempts to move the file using $_FILES['file']['name']. This specific value represents the filename as submitted by the client browser, not the path to the actual file on the server's filesystem.

When a user selects a file and submits a form, PHP receives the file contents but cannot determine what directory it should reside in without explicit instruction. Consequently, PHP stores these uploads in a temporary location defined by php.ini. By default, this is usually /tmp on Linux or a specific temp directory on macOS. The variable $_FILES['file']['tmp_name'] holds the path to this actual temporary file (e.g., /tmp/php12345.tmp). Conversely, $_FILES['file']['name'] might contain something like "image.jpg".

Attempting to pass the original name (image.jpg) to move_uploaded_file() as the source argument causes PHP to look for a file literally named "image.jpg" in the current working directory. Since this file does not exist there, the function returns false and issues a warning about moving a non-existent file. To make the upload work, you must use the temporary path stored in tmp_name.

Correcting the Destination Path Logic

Beyond simply fixing the source argument, the destination path logic in your example is also flawed. The code assigns $move = "/Users/George/Desktop/uploads/"; directly to a variable meant to hold the destination directory, but then passes $move as the destination argument to move_uploaded_file(). While move_uploaded_file() accepts a destination filename, it expects the destination path to be absolute or relative to the current script's execution context. Hardcoding system-specific paths like /Users/George/Desktop/... works only on that specific machine and fails immediately in any other environment.

Furthermore, moving a file requires that the target directory exists and is writable by the web server process (usually www-data, apache, or _httpd). If the uploads folder does not exist, PHP will throw a "No such file or directory" error before attempting the move.

Here is how to correct the logic in upload_file.php:

  1. Create the destination directory if it doesn't already exist using mkdir() and chmod().
  2. Use tmp_name for the source argument.
  3. Generate a unique filename for the destination to prevent overwriting existing files.
<?php
// Define configuration
$uploadDir = '/var/www/uploads/'; // Use server-appropriate paths, not local desktop paths
$maxSize = 5 * 1024 * 1024; // 5MB limit in bytes

// Ensure the directory exists and is writable
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0755, true);
}

// Check if a file was uploaded
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
    
    $fileName = $_FILES['file']['name'];
    $tmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];

    // Check file size against limit before moving
    if ($fileSize > $maxSize) {
        echo "File is too large.";
        exit;
    }

    // Create a unique filename to prevent collisions
    $newFileName = uniqid() . '_' . basename($fileName);
    $destinationPath = $uploadDir . $newFileName;

    // Move the uploaded file from temp storage to the destination
    if (move_uploaded_file($tmpName, $destinationPath)) {
        echo "File uploaded successfully: " . $newFileName;
    } else {
        echo "Failed to move uploaded file.";
    }
} else {
    echo "No valid file uploaded or upload error occurred.";
}
?>

Addressing Form Configuration Issues

Your HTML form snippet contains a configuration that will prevent uploads from working entirely. You have included an input field named MAX_FILE_SIZE:

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

This directive instructs the PHP script to reject any file larger than 30,000 bytes (roughly 30KB). If a user attempts to upload a standard image or document, the server will immediately reject it with an error before your PHP logic even runs. Unless you are specifically testing with tiny files, you should remove this element from your form and rely on php.ini settings (upload_max_filesize) or runtime checks in your PHP code to enforce limits.

Additionally, ensure that php.ini has the following directives enabled and set appropriately:

  • file_uploads = On
  • upload_max_filesize = 10M (or higher)
  • post_max_size = 10M (must be larger than upload_max_filesize)

Security Considerations for File Uploads

Simply moving files is rarely enough for a production application. Your current approach saves the file with the original client-provided name (image.jpg). This opens the door to security vulnerabilities such as path traversal attacks or uploading malicious scripts disguised as images (e.g., shell.php.jpg).

Best practices dictate:

  1. Validate File Types: Check the MIME type using finfo_file() or the extension against a whitelist of allowed extensions (e.g., .jpg, .png).
  2. Rename Files: Always rename the uploaded file to something random (using uniqid()) and store it in a directory outside the web root if possible, or ensure the directory has no executable permissions.
  3. Sanitize Names: Strip out any path information from $_FILES['file']['name'] before processing, as PHP will ignore the path component but using the raw name can lead to logic errors.

For developers moving away from raw procedural PHP, Laravel provides robust helpers that handle these complexities automatically. The Storage facade in Laravel abstracts away the filesystem operations and includes built-in validation rules for file types and sizes, significantly reducing the risk of implementation errors found in custom scripts.

Final Verification Steps

If you have corrected the code above and verified your php.ini settings, test the upload using a slightly larger file than 30KB to bypass the hidden limit in your original HTML form. If the error persists, check the server error log (/var/log/apache2/error.log or similar) for specific messages regarding permission denied errors on the destination directory. Ensuring the web server user owns the uploads folder with write permissions (typically 755) is often the missing piece in these scenarios.

Our network