Joseph_J Posted September 23, 2013 Share Posted September 23, 2013 Hello,Code works great across site except for this modification I am trying to make:I want to save the images to the db which I have successfully done.However, I can not get the image to display. Any suggestions would be helpful!btw: The binary code does display in the site where the picture is suppose to be.file.php: <div class="widget"> <h2>Hello, <?php echo $user_data['first_name']?>!</h2> <div class="inner"> <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']; $file_size = getimagesize($_FILES['profile']['tmp_name']); $image = addslashes(file_get_contents($_FILES['profile']['tmp_name'])); if (in_array($file_extn, $allowed) === false) { $errors[] = 'Not a valid file type! Please enter an image with a .jpg, .jpeg, .gif, or a .png file extension!'; } if ($file_size == false) { $errors[] = 'This file is not a valid image!'; } if(empty($errors) === true) { change_profile_image($session_user_id, $image); echo "<script>window.location.href='index.php';</script>"; header('Location: index.php'); exit(); } else { echo output_errors($errors); } } } if (empty($user_data['profile']) === false) { echo '<img src="', $user_data['profile'], '" alt="', $user_data['first_name'], '\'s Profile Image">'; } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="profile"> <input type="submit" value="Upload"> </form> </div> <ul> <li> <a href="logout.php">Log out</a> </li> <li> <a href="/<?php echo $user_data['username']; ?>">Profile</a> </li> <li> <a href="changepassword.php">Change password</a> </li> <li> <a href="settings.php">Settings</a> </li> </ul> </div> </div> called function: $user_id is contained in $session_user_id <?php function change_profile_image($user_id, $image) { if (!$insert = mysql_query("UPDATE `users` SET `profile` = '$image' WHERE `user_id` = $user_id")) { echo "Problem uploading image!"; } } ?> Thanks for your time! ~A noob Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/ Share on other sites More sharing options...
vinny42 Posted September 23, 2013 Share Posted September 23, 2013 The binary code does display in the site where the picture is suppose to be. Are you using an IMG tag to call the image, and is does the script that fetches the image send the apripriate headers? Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450756 Share on other sites More sharing options...
Joseph_J Posted September 23, 2013 Author Share Posted September 23, 2013 I guess the answer to both of those is no. You are looking at the entire code for what I am doing! Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450757 Share on other sites More sharing options...
mac_gyver Posted September 23, 2013 Share Posted September 23, 2013 you are trying to output the binary image data in the <img tag. the src=' ... ' attribute is a URL to the image. it's the browser that fetches the image and renders/displays the image on the web page. to dynamically output an image, you would put the url of a .php script into the the src=' ... ' attribute. the url would have the image's database id on the end of it - ?id=123. the .php script would take that id, perform any validation, permissions check, sql injection protection, then retrieve the image mime type and image data for the requested image, output a content-type header using the mime type and output the binary image data. Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450794 Share on other sites More sharing options...
Ch0cu3r Posted September 23, 2013 Share Posted September 23, 2013 To get you script to display the uploaded profile picture properly you'll need to do as max_qyver suggested, There is another approach to this and that's to not to store actual raw image in the database. When the image is uploaded save the image to servers file system, for example http://mysite.com/images/profiles/<users-profile-image-name>.jpg. Store only this file path to the database. Your existing code for displaying the image should work. So you should be able to change this line $image = addslashes(file_get_contents($_FILES['profile']['tmp_name'])); to // where to save the uploaded image to $image = './images/profiles/' . $_FILES['profile']['name']; // save the uploaded file to ./images/profiles foler move_uploaded_file($_FILES["file"]["tmp_name"], $SERVER['DOCUMENT_ROOT'] . $image); $image will not contain a file path to the uploaded image. You save this path to the database. This line echo '<img src="', $user_data['profile'], '" alt="', $user_data['first_name'], '\'s Profile Image">'; should work as expected Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450814 Share on other sites More sharing options...
KevinM1 Posted September 23, 2013 Share Posted September 23, 2013 This kind of headache illustrates exactly why it's better to store files in the file system than the database. Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450842 Share on other sites More sharing options...
vinny42 Posted September 23, 2013 Share Posted September 23, 2013 This kind of headache illustrates exactly why it's better to store files in the file system than the database. +42. If you're not doing anything with processing or distribution then files go in the filesystem. That said, you might be able to put the binary data of the image in the tag itself, which would reduce the number of hits your server takes (but if you want to reduce hits you have a busy server and a busy server shouldn't have a huge datafile for the database unles it's a big server...) Quote Link to comment https://forums.phpfreaks.com/topic/282370-blob-fialed-to-display/#findComment-1450849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.