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
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
//php
";
$uploadOk = 0;
}
} else {
die("No file was uploaded or invalid file input.");
}
// Check file size (optional)
if ($_FILES["profilePicture"]["size"] > 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;
?>
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sangeetha SPosted Nov 26, 2024, 5:09 AM
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
PHP Code (upload_file.php)