PHP Ajax文件未上传至000webhost数据库问题排查求助
Hey Tommy, let's dig into why your file isn't showing up on the server even though the correct path is saved in the database. From the code snippet you shared, it looks like you're handling text form fields (like ideaTopic and ideaCategories) perfectly fine—but you're missing the critical steps to actually save the uploaded file to your server. Here's what you need to fix:
Key Missing Steps & Solutions
Ensure your form uses the right enctype
Your HTML form must includeenctype="multipart/form-data", otherwise browsers won’t send file data to the server. Without this, PHP’s$_FILESsuperglobal will be empty, and you can’t access the uploaded file. Example:<form action="submit.php" method="POST" enctype="multipart/form-data"> <!-- Your existing text fields --> <input type="text" name="ideaTopic"> <!-- File input field --> <input type="file" name="ideaFile"> <button type="submit">Submit</button> </form>Add logic to handle the uploaded file with
$_FILES
Right now, you’re probably just saving a manually defined path to the database without actually moving the uploaded file from PHP’s temporary directory to your target folder. Here’s how to implement this correctly:// First, check if a file was uploaded without errors if (isset($_FILES['ideaFile']) && $_FILES['ideaFile']['error'] === UPLOAD_ERR_OK) { // Define your target upload directory (use absolute path to avoid issues) $uploadDir = __DIR__ . '/uploads/'; $fileName = basename($_FILES['ideaFile']['name']); $targetPath = $uploadDir . $fileName; // Create the upload directory if it doesn't exist if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); // 0755 is secure for most environments } // Move the temporary file to your target directory (this is the critical step!) if (move_uploaded_file($_FILES['ideaFile']['tmp_name'], $targetPath)) { // Now save the valid $targetPath to your database $ideaTopic = $_POST['ideaTopic']; $ideaCategories = $_POST['ideaCategories']; // ... rest of your database insertion code using $targetPath echo "File uploaded successfully!"; } else { echo "Failed to save the file. Check directory permissions!"; } } else { // Handle common upload errors switch ($_FILES['ideaFile']['error']) { case UPLOAD_ERR_INI_SIZE: echo "File exceeds the upload_max_filesize limit in php.ini"; break; case UPLOAD_ERR_NO_FILE: echo "No file was selected for upload"; break; default: echo "An unknown error occurred during upload"; } }Verify directory permissions
The folder you’re uploading to must be writable by the PHP process (usually thewww-dataorapacheuser on most servers). You can set permissions via terminal or FTP:chmod 755 /path/to/your/uploads/directoryAvoid using
777(world-writable) in production—it’s a major security risk.Check PHP upload limits
Make sure yourphp.inisettings allow the file size you’re trying to upload:upload_max_filesize: Maximum size per individual filepost_max_size: Maximum size for the entire POST request (should be larger thanupload_max_filesize)
You can confirm these values withphpinfo()if you’re unsure.
Why Your Current Code Isn’t Working
Right now, you’re saving a file path to the database, but you never actually take the temporary file that PHP automatically creates when a user uploads something and move it to that path. Temporary files get deleted automatically after the script finishes running, so nothing ends up in your target directory.
Once you add the move_uploaded_file() logic and properly handle the $_FILES data, your files should appear in the specified directory, matching the path stored in the database.
内容的提问来源于stack exchange,提问作者Tommy Chong

