drayarms Posted May 7, 2011 Share Posted May 7, 2011 I came up with the following script for displaying an uploaded picture on a webpage but for some reason I can't pinpoint, the picture won't be displayed. I checked my database from php myadmin and sure enough, the picture was successfully uploaded but somehow, the picture won't be displayed. So here is the display script. I named it display_pic.php <?php //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'); //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 image_cartegory = 'main' "); $row = mysql_fetch_assoc($rs); $imagebytes = $row[image]; header("Content-type: image/jpeg"); print $imagebytes; ?> The tag on the html page that's supposed to display the picture reads something like this <img src="display_pic.php" width="140" height="140"> And just in case this might help, I will include the image upload script below, which I think worked just fine because my values were successfully inserted into the database. <?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. ?> So any insights as to why the script fails to display the image? Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/ Share on other sites More sharing options...
wildteen88 Posted May 7, 2011 Share Posted May 7, 2011 What happens when you go to display_pic.php do any errors appear? Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212049 Share on other sites More sharing options...
fugix Posted May 7, 2011 Share Posted May 7, 2011 are you saving the actual image into just the db or a directory as well? Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212053 Share on other sites More sharing options...
fugix Posted May 7, 2011 Share Posted May 7, 2011 nevermind i see that its just the directory, what i like to do is save the file into a directory, and save the path to that directory in my db...then use the image path in my db as the src of an <img> tag Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212055 Share on other sites More sharing options...
spiderwell Posted May 7, 2011 Share Posted May 7, 2011 $imagebytes = $row[image]; should be $imagebytes = $row['image']; @fugix, the upload script appears to be saving the image as a massive string in the database, never seen that before, personally i do what you suggested. only i keep the path in my 'common' include, and save image name in db Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212092 Share on other sites More sharing options...
drayarms Posted May 8, 2011 Author Share Posted May 8, 2011 @wildteen98, no error message appears. the image div where the image is supposed to appear just displays a tiny jpeg icon right at the center @fugix, i'm saving directly into the database. i first want to see if i can successfully save and retrieve directly from the database b4 trying the directory method. do u have any script that demonstrates how to save the file in a directory and save the path in the database? @spiderwell, i tried ur suggestion $imagebytes = $row['image']; no luck with that. please more suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212292 Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 my suggestion is to scrap trying to save the image in the database and just save all images in a directory. Quote Link to comment https://forums.phpfreaks.com/topic/235797-image-display-script-wont-work/#findComment-1212310 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.