drayarms Posted February 19, 2011 Share Posted February 19, 2011 I put together the following blocs of code for uploading pictures into a database and displaying them on a webpage. The pictures are supposed to be displayed on the member's only page of a website I'm working on, upon logging in, and they are supposed to be the member's uploaded picture. I created several members and and used one of my existing member accounts to test the uploading process. The picture upload process appeared to have been successful when I checked on myphpadmin. Yet, when I login with this account, no picture is displayed, instead, a tiny jpg icon is displayed at the top left corner of the box in which the picture was supposed to be displayed. Same thing when I login with the other accounts with which I haven't yet uploaded a picture. I'll start with the code that installs the table in the database $query = "CREATE TABLE images ( image_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , member_id INT UNSIGNED, like_id INT UNSIGNED, image LONGBLOB NOT NULL, image_name varchar(255) NOT NULL, image_type varchar(4) NOT NULL, image_size int( NOT NULL, image_cartegory VARCHAR(20) NOT NULL, image_path VARCHAR(300), image_date DATE )"; Then here is the code which allows the member to upload his picture: <form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="102400" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <input value="Submit" type="submit"> </form> And here is the insertimage.php which inserts the image into our database: Note that I have to authenticate the user in order to register his session id which is used later on in the select query to identify him and select the right image that corresponds to him. <?php //This file inserts the main image into the images table. //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //authenticate user //Start session session_start(); //Connect to database require ('config.php'); //Check whether the session variable id is present or not. If not, deny access. if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) { header("location: access_denied.php"); exit(); } else{ // Make sure the user actually // selected and uploaded a file if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); // Create the query and insert // into our database. $query = "INSERT INTO images (member_id, image_cartegory, image_date, image) VALUES ('{$_SESSION['id']}', 'main', NOW(), '$data')"; $results = mysql_query($query); // Print results print "Thank you, your file has been uploaded."; } else { print "No image selected/uploaded"; } // Close our MySQL Link mysql_close(); } //End of if statmemnt. ?> On the page which is supposed to display the image upon login in, I inserted the following html code in the div that's supposed to contain the image: <div id="image_box" style="float:left; background-color: #c0c0c0; height:150px; width:140px; border- color:#a0a0a0;border-style:outset;border-width:1px; margin:auto; "> <img src=picscript.php?imname=potwoods> </div> And finally, the picscript.php contained the select query: <?php //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //include the config file require('config.php'); $image = stripslashes($_REQUEST[imname]); $rs = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND cartegoty = 'main' "); $row = mysql_fetch_assoc($rs); $imagebytes = $row[image]; header("Content-type: image/jpeg"); print $imagebytes; ?> Now the million dollar question is, "What is preventing the picture from getting displayed?" I know this is a very lengthy and laborious problem to follow but I'm sure there is someone out there who can point out where I'm not getting it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 19, 2011 Share Posted February 19, 2011 What does a phpinfo() statement show for the magic_quotes_runtime setting? Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176652 Share on other sites More sharing options...
Fergal Andrews Posted February 19, 2011 Share Posted February 19, 2011 hi drayarms, Try changing $imagebytes = $row[image]; to $imagebytes = $row['image']; If that doesn't work check that nothing is being outputted before header("Content-type: image/jpeg"); cheers , Fergal Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176653 Share on other sites More sharing options...
PFMaBiSmAd Posted February 19, 2011 Share Posted February 19, 2011 Actually, upon further review, you don't have a session_start() statement in picscript.php, so $_SESSION['id'] won't exist and your query won't match anything. Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176654 Share on other sites More sharing options...
hyster Posted February 19, 2011 Share Posted February 19, 2011 quick look i seen this. image_cartegory imcartegoryage_cartegory VARCHAR(20) NOT NULL, cartegoty $rs = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND cartegoty = 'main' "); Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176657 Share on other sites More sharing options...
PFMaBiSmAd Posted February 19, 2011 Share Posted February 19, 2011 Because of the multiple problems in the picscript.php code, I recommend temporarily commenting out the header() statement and browsing directly to the picscript.php file. This will show you the actual output, including any php detected errors. You also need to use E_ALL for your error reporting setting (by using & ~E_NOTICE you are hiding notice messages, which can also hide problems in your code that is preventing it from working.) Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176662 Share on other sites More sharing options...
drayarms Posted February 19, 2011 Author Share Posted February 19, 2011 thanks hyster, you pointed out the real problem. now my image displays beautifully. Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1176826 Share on other sites More sharing options...
drayarms Posted February 20, 2011 Author Share Posted February 20, 2011 also i forgot to mention that i also included the session_start() statement at the begining of the the picsript.php as suggested by PFMaBiSmAd in addition to correcting the image_cartegory typo. thanks all for pointing out my errors. Quote Link to comment https://forums.phpfreaks.com/topic/228174-image-stored-in-database-cant-be-displayed/#findComment-1177087 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.