PHP上传图片失败:file_get_contents无法打开流的问题求助
Hey Ahmed, let's work through that file not found error you're hitting when trying to upload your product image and insert data into the produit table. The warning tells us PHP can't locate the image file you're trying to read with file_get_contents()—here's how to fix it:
1. First, confirm your HTML form is set up for file uploads
If your form doesn't include the enctype="multipart/form-data" attribute, files won't be sent to the server at all. Double-check your form code looks like this:
<form action="upload.php" method="POST" enctype="multipart/form-data"> <!-- Product info fields --> <input type="text" name="product_id" placeholder="Product ID"> <input type="text" name="product_name" placeholder="Product Name"> <!-- Image upload field --> <input type="file" name="product_image"> <button type="submit">Add Product</button> </form>
Without that enctype attribute, $_FILES (PHP's file upload superglobal) will be empty, and you'll never find the file.
2. Use the correct temporary file path from $_FILES
When you upload a file, PHP stores it in a temporary directory on your server—not using the original filename you see in the warning. You need to reference this temporary path instead of the original filename.
Wrong (what you're doing now):
// This looks for the file in the same folder as upload.php, which it's not $image_content = file_get_contents("26754765_956068941216475_1243103994_n.jpg");
Correct:
// First, check if the file uploaded successfully if ($_FILES['product_image']['error'] !== UPLOAD_ERR_OK) { echo "Upload failed with error code: " . $_FILES['product_image']['error']; exit; } // Use the temporary file path provided by PHP $tmp_file_path = $_FILES['product_image']['tmp_name']; $image_content = file_get_contents($tmp_file_path);
The tmp_name value in $_FILES is the actual path to the uploaded file on your server.
3. (Optional but recommended) Move the file to a permanent directory
Temporary files get deleted automatically once your PHP script finishes running. If you want to keep the image for future use (like displaying it on your site), move it to a dedicated uploads folder:
// Define your permanent upload directory (create it if it doesn't exist) $upload_dir = __DIR__ . '/uploads/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } // Generate a unique filename to avoid overwriting existing files $unique_filename = uniqid() . '_' . $_FILES['product_image']['name']; $permanent_path = $upload_dir . $unique_filename; // Move the temporary file to the permanent location if (move_uploaded_file($tmp_file_path, $permanent_path)) { // Now you can use $permanent_path to read the image or store it in your database $image_content = file_get_contents($permanent_path); } else { echo "Failed to save the uploaded image."; exit; }
4. Check directory permissions
Make sure the folder where you're trying to read/write files (either the temporary directory or your custom uploads folder) has the correct permissions. For XAMPP on Windows, the htdocs/PFE folder should already have proper access, but if you created a custom uploads folder, confirm Apache has read/write permissions for it.
Once you fix the file path issue, you should be able to read the image content and proceed with inserting all your product data into the produit table.
内容的提问来源于stack exchange,提问作者ahmed

