I Had written a previous post about this but since then I have made some changes and attempted other things. I am trying to allow users to upload a profile image to which can be displayed on their profile.
However I am able to get the upload script working, but when trying to display the image I seem to get a image box. I have come to find that the database table updates to the new image and saves it in a folder, then when trying to display that image I am either able to echo out the file name or just a blank image box.
Any help would be much appreciated (Y)
PHP to display image:
<?php echo '<img src="', $user_data['profile'], '" alt="', $user_data['name'], '\'s Profile Image">'; ?>
PHP Image upload script (This works and I believe It may be something in the functions script):
<?php
if (isset($_FILES['profile']) === true) {
if (empty($_FILES['profile']['name']) === true) {
?>
<p>You have not chosen a image</p>
<?php
} else {
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['profile']['name'];
$file_extension = end((explode('.', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extension, $allowed) === true) {
change_profile_image($session_user_id, $file_temp, $file_extension);
header('Location: profile.php?success');
exit();
} else {
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
if (empty($user_data['profile']) === false) {
echo '<img class="profileimg" src="', $user_data['profile'], '" alt="', $user_data['name'], '\'s Profile Image">';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile">
<div class="submit3">
<input type="submit" value="Upload" name="complete"/>
</div>
</form>
Functions script:
function change_profile_image($user_id, $file_temp, $file_extension) {
$file_path = 'images/profile/' . substr(md5(time()), 0, 10) . '.' . $file_extension;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `users` SET `profile` = '" . mysql_real_escape_string($file_path) . "' WHERE `user_id` = " . (int)$user_id);}
I believe it has something to do with the functions script but after days of changing the script I still cannot display the image after uploading. PS: Yes I know I should be using MSQLI functions, but I am just trying to finish off a college project to brush up on my PHP knowledge.