Hi Team
I need some help, want to upload an display an image using php and html the code is not displaying an image when uploaded. the back end works fine.
// html
Code
Sam Anderson
Student Id: ST17241
Choose an image to upload:
Upload Image//php
PHP
500000) {
echo "Sorry, your file is too large.
";
$uploadOk = 0;
}
// Allow certain file formats
if (!in_array($imageFileType, ["jpg", "jpeg", "png", "gif"])) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.
";
$uploadOk = 0;
}
// Attempt to upload the file
if ($uploadOk == 1) {
// Generate a unique filename for the uploaded image
$targetFile = $targetDir . uniqid("profile_") . "." . $imageFileType;
if (move_uploaded_file($_FILES["profilePicture"]["tmp_name"], $targetFile)) {
// Store the filename in the session to remember the uploaded image
$_SESSION['profile_image'] = $targetFile;
echo "The file has been uploaded.
";
} else {
echo "Sorry, there was an error uploading your file.
";
}
} else {
echo "File upload failed.
";
}
// Redirect back to the dashboard
header("Location: dashboard-user.php");
exit;
?>
Sangeetha SRank #212 · 11.4K reputationPosted Nov 26, 2024, 12:09 AM EST
Hi,
imagesdirectory has the correct permissions to allow file uploads.Make sure your HTML form and image display code are set up properly.
dashboard-user.php
" alt="user" id="profile-img">PHP Code (upload_file.php)
500000) { echo "Sorry, your file is too large. "; $uploadOk = 0; } // Allow certain file formats if (!in_array($imageFileType, ["jpg", "jpeg", "png", "gif"])) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed. "; $uploadOk = 0; } // Attempt to upload the file if ($uploadOk == 1) { // Generate a unique filename for the uploaded image $targetFile = $targetDir . uniqid("profile_") . "." . $imageFileType; if (move_uploaded_file($_FILES["profilePicture"]["tmp_name"], $targetFile)) { // Store the filename in the session to remember the uploaded image $_SESSION['profile_image'] = $targetFile; echo "The file has been uploaded. "; } else { echo "Sorry, there was an error uploading your file. "; } } else { echo "File upload failed. "; } // Redirect back to the dashboard (make sure to check where you want to go) header("Location: dashboard-user.php"); exit(); ?>