Vinodpv Posted April 9, 2014 Share Posted April 9, 2014 Hi, Below is my image browse upload code , here I can insert image in database, but it is dispaly only icon instead of image, Please help me to display image...many many thanks in advance index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Logo Upload here..</title></head><body><form action="index.php" method="post" enctype="multipart/form-data"><label>Product Name</label><input type="text" name="pname" /><label>Product image</label><input type="file" name="image" ><input type="submit" name="submit" value="Add Logo"></form><?phpinclude "db_conexn.php";if(!isset($_FILES["image"]["tmp_name"]))echo "Please select an image..";else{$image=addslashes(file_get_contents($_FILES["image"]["tmp_name"]));$image_name=addslashes($_FILES["image"]["name"]);$image_size=getimagesize($_FILES["image"]["tmp_name"]); if($image_size==FALSE) echo "It is not an image"; else { $insert=mysql_query("insert into images values('','$image_name','$image')"); echo "<img src='uploadsql.php'>"; }}?></body></html> uploadsql.php <html><body><?php$result=mysql_query("select * from images");while($row=mysql_fetch_array($result)){header("Content-Type:image/jpeg");$image=$row['image'];echo $image;}?></html> table table : images id name image 26 Chrysanthemum.jpg [bLOB - 858.8 KiB] Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/ Share on other sites More sharing options...
jazzman1 Posted April 9, 2014 Share Posted April 9, 2014 Can you post the output of: <?php $result=mysql_query("select * from images where id = 26"); while($row=mysql_fetch_assoc($result)) { echo $row['image'].'<br />'; echo $row['name']; } ?> Use the forum code [ code ] tags when providing code. Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475475 Share on other sites More sharing options...
Ch0cu3r Posted April 9, 2014 Share Posted April 9, 2014 (edited) Don't store the images in the database, this makes things more complex later. You are best of saving the uploaded image to the filesystem, look at using move_uploaded_file to do this. And then only save the image path in the database. eg $image_dir = 'images/'; // where images are to be stored $image_name=mysql_real_escape_string($_FILES["image"]["name"]); $image_path = $image_dir . $image_name; // image path, this path is saved in the database // move the uploaded file, to the image dir if(move_uploaded_file($_FILES["image"]["tmp_name"], $image_path)) { $insert=mysql_query("insert into images values('','$image_name','$image_path')"); if(!$insert) echo 'Database error: ' . mysql_error()); } else { echo 'Unable to move '.$_FILES["image"]["name"].' to '.$image_dir; } Now to display the image you can echo the image path into the src attribute for the <img> tag, eg <?php $result=mysql_query("select * from images"); while($row=mysql_fetch_array($result)) { echo '<img src="'.$row['image'].'" />'; } ?> Also dont use addslashes to sanitize user input use mysql_real_escape_string or better yet convert your code to mysqli or PDO and use prepared queries Edited April 9, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475476 Share on other sites More sharing options...
Vinodpv Posted April 9, 2014 Author Share Posted April 9, 2014 Don't store the images in the database, this makes things more complex later. You are best of saving the uploaded image to the filesystem, look at using move_uploaded_file to do this. And then only save the image path in the database. eg $image_dir = 'images/'; // where images are to be stored $image_name=mysql_real_escape_string($_FILES["image"]["name"]); $image_path = $image_dir . $image_name; // image path, this path is saved in the database // move the uploaded file, to the image dir if(move_uploaded_file($_FILES["image"]["tmp_name"], $image_path)) { $insert=mysql_query("insert into images values('','$image_name','$image_path')"); if(!$insert) echo 'Database error: ' . mysql_error()); } else { echo 'Unable to move '.$_FILES["image"]["name"].' to '.$image_dir; } Now to display the image you can echo the image path into the src attribute for the <img> tag, eg <?php $result=mysql_query("select * from images"); while($row=mysql_fetch_array($result)) { echo '<img src="'.$row['image'].'" />'; } ?> Also dont use addslashes to sanitize user input use mysql_real_escape_string or better yet convert your code to mysqli or PDO and use prepared queries thank you friend it is working good, image is uploading in folder and database both. Can u please tell me how to display it Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475479 Share on other sites More sharing options...
jazzman1 Posted April 9, 2014 Share Posted April 9, 2014 If the binary string is stored in a database as expected, your script should work properly. Try my script and add the header function with content-type to it. $result=mysql_query("select * from images where id = 26"); $row=mysql_fetch_assoc($result); header("Content-Type:image/jpeg"); echo $row['image']; Much better is to use application server store the images instead the database as Ch0cu3r already stated.It's also not recommended to use the old mysql extension for new development, use either the mysqli or pdo_mysql Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475484 Share on other sites More sharing options...
Vinodpv Posted April 10, 2014 Author Share Posted April 10, 2014 index.php <tr> <td height="31">logo Upload </td><td><input type="file" name="image" /></td></tr> db.php $image_dir = 'imagess/';$image_name=mysql_real_escape_string($_FILES["image"]["name"]);$image_path = $image_dir . $image_name;if(!move_uploaded_file($_FILES["image"]["tmp_name"], $image_path)){ echo 'Unable to move '.$_FILES["image"]["name"].' to '.$image_dir;}else{ $insert = mysql_query("INSERT INTO `company`(comp_name,comp_address,branch,user_from,photo) VALUES ('$compName','$compAddress','$branch','$sub','$image_name')"); } This is my working code .. image will save in database and folder, Can u please help me for updation image using update query . And display this image ... Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475642 Share on other sites More sharing options...
Ch0cu3r Posted April 10, 2014 Share Posted April 10, 2014 Does your company table have an id field? That will be the field you'd reference the row in the WHERE clause for the UPDATE/SELECT query, eg example for select <?php $company_id = intval(.. company id val..); // get the row that matches the company id $result=mysql_query("select * from company where id=$company_id"); if(mysql_num_rows($result) > 0) { { $row=mysql_fetch_assoc($result); // output image path in the src attribute echo '<img src="'.$row['photo'].'" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287631-image-browse-upload-not-displaying-using-php/#findComment-1475664 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.