move_uploaded_file函数失效求助:已排查路径与权限仍报错
move_uploaded_file Not Working Hey there, let's work through why your move_uploaded_file call is failing—you've already checked paths and permissions, so let's cover the other common culprits that might be tripping you up.
Key Checks to Fix the Issue
1. Confirm the Temporary File is a Valid Upload
move_uploaded_file has a strict security rule: it only moves files uploaded via HTTP POST. If your $tempN variable isn't pulled directly from $_FILES['your_input_name']['tmp_name'], the function will fail outright (even if the path looks correct).
Add this quick check to verify:
if(is_uploaded_file($tempFileName)) { echo "This is a valid uploaded temporary file"; } else { echo "Not an uploaded file—this is why move_uploaded_file is failing!"; }
Always make sure $tempFileName comes from the $_FILES superglobal, not a manually defined path.
2. Use Absolute Paths to Avoid Ambiguity
Relative paths like "files/" can be unreliable because they depend on PHP's current working directory (which might not match your script's location). Switch to an absolute path using __DIR__ (the directory of your current PHP script) to eliminate confusion:
// Define absolute path to your target directory $target = __DIR__ . "/files/"; // Create the directory if it doesn't exist (with proper permissions) if(!is_dir($target)) { mkdir($target, 0755, true); // Recursively creates nested directories } // Sanitize the filename to prevent path traversal attacks $safeFileName = basename($teste); $fileTarget = $target . $safeFileName;
3. Double-Check Permissions for the Right User
You mentioned checking permissions, but ensure the target directory is owned by the user running PHP (usually www-data for Apache/Nginx on Linux, or IIS_IUSRS on Windows).
On Linux, run these commands to verify and fix permissions:
# Check directory owner and permissions ls -l files/ # Change owner to PHP's user (adjust if your server uses a different user) sudo chown www-data:www-data files/ # Set correct permissions sudo chmod 0755 files/
4. Get Specific Error Messages
Right now you're only outputting "erro"—enable PHP error reporting to see exactly why the function is failing:
// Add this at the top of your script error_reporting(E_ALL); ini_set('display_errors', 1); // When move_uploaded_file fails, print the last error if(move_uploaded_file($tempFileName, $fileTarget)){ echo "<br>I am there yeeee"; }else{ echo "<br>erro: " . error_get_last()['message']; }
This will show you details like "Permission denied" or "No such file or directory", which directly point to the root problem.
5. Check PHP Upload Limits
If your file is too large, PHP might reject the upload entirely—meaning the temporary file never exists. Verify these settings in your php.ini file (or use phpinfo() to check current values):
upload_max_filesize: Maximum size of a single uploaded filepost_max_size: Total size allowed for POST requests (must be larger thanupload_max_filesize)max_execution_time: Ensure this is long enough for large files to upload
Modified Working Code Example
Here's a revised version of your code incorporating all these checks:
error_reporting(E_ALL); ini_set('display_errors', 1); // Replace 'your_file_input' with the name attribute from your file upload form $uploadedFile = $_FILES['your_file_input']; $targetDir = __DIR__ . "/files/"; if(!is_dir($targetDir)) { mkdir($targetDir, 0755, true); } $safeFileName = basename($uploadedFile['name']); $targetPath = $targetDir . $safeFileName; $tempFilePath = $uploadedFile['tmp_name']; if(is_uploaded_file($tempFilePath)) { if(move_uploaded_file($tempFilePath, $targetPath)){ echo "<br>I am there yeeee"; }else{ echo "<br>erro: " . error_get_last()['message']; } } else { echo "<br>Error: The temporary file is not a valid uploaded file. Did the file even upload?"; }
内容的提问来源于stack exchange,提问作者Frederico Calor

