hardeep hardeep - 7 months ago 12
PHP Question

how do i retrieve image from database from the code i am using?

hi i have already typed my code below. the database is saving the path of image but it is not displaying the image . where could i be wrong?

<div class="profile">
<?php
if (isset($_FILES['profile']) === true) {
if (empty($_FILES['profile']['name']) === true) {
echo 'Please choose a file';
} else {
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['profile']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extn, $allowed) === true) {
///upload file.
change_profile_image($session_user_id, $file_temp, $file_extn);
} else {
echo 'incorrect file type. Allowed formats: ';
echo implode(', ', $allowed);
}
}
}
if (empty($user_data['profile']) === false) {
echo '<img src"', $user_data['profile'], '"
alt="', $user_data['first_name'], '\'s">';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile">
<input type="submit">
</form>
</div>


and the function change_profile_image I am calling is below:

function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = 'images/profile/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
//echo $file_path;
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);
}


the database is saving the image path but not displaying the image on the webpage.

Answer

Where you've written

echo '<img src"', $user_data['profile'], '"

change it to

echo '<img src="'.$user_data['profile'].'"

Note the added equals sign and that full-stops instead of commas are used for concatenation.