ktalebian Posted August 28, 2007 Share Posted August 28, 2007 Hi I have uploaded images on my mySQL db using PHP. Now I want to display them. The code I use is <?php ob_start(); // Connected to db $id = $_GET["id"]; $id = 4; function clean($input, $maxlength) { $input = substr($input, 0, $maxlength); $input = EscapeShellCmd($input); return ($input); } $file = clean($file, 4); $query = "SELECT name, type, content FROM `upload` WHERE id = '$id'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $type = $row["type"]; $name = $row["name"]; $content = $row["content"]; } // Output the MIME header header("Content-Type: {$type }"); // Output the image echo $content; ?> The problem with this code is that this has to be the first thing in the page. Also, anything that comes after it is not displayed! Why? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/ Share on other sites More sharing options...
Fadion Posted August 28, 2007 Share Posted August 28, 2007 If $row['content'] is the path to the image, then why not use: echo "<img src=\"{$row['content']}\" />" In working with images, ive usually set the content-type header before outputing an image with imagejpeg() or similiar functions, so im not sure it will work with just an echo. U can consider using gd functions if u cant get this to work: header("Content-Type: image/jpeg"); $imgHandler = imagecreatefromjpeg($row['content']); imagejpeg($imgHandler); Still mentioning, if $row['content'] is the path to the image. Imagecreatefromjpeg() will copy the image and create an image resource which is outputed to the browser using imagejpeg. There are also imagecreatefromgif() imagecreatefrompng() and imagegif() imagepng(). Also when making a query find an ID, u know there is only one returned record so u dont need a while loop. Just "$row=mysql_fetch_array($result);" will do it. Hope it was helpful. Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-335941 Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2007 Share Posted August 28, 2007 If you are trying to display multiple images, you need to put the code that displays the image in the while loop. I'm not sure why you have the header function there, but if there is a different $type for each image, that needs to go into the while loop as well. Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-335945 Share on other sites More sharing options...
ktalebian Posted August 28, 2007 Author Share Posted August 28, 2007 Hi Ok, none of those codes worked! The image is stored in the db, so I cannot use img src. As for the header("Content-Type: image/jpeg"); $imgHandler = imagecreatefromjpeg($row['content']); imagejpeg($imgHandler); when I run it, it says function imagecreatefromjpeg is not defined! Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-335980 Share on other sites More sharing options...
Fadion Posted August 28, 2007 Share Posted August 28, 2007 look up imagecreatefromjpeg in the php manual. $row['content'] is a string with the image path or what? EDIT: Do u have GD Installed? Use phpinfo() to verify it. Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-336755 Share on other sites More sharing options...
ktalebian Posted August 29, 2007 Author Share Posted August 29, 2007 Thanks for the reply: Ok, using phpinfo, I have this Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared" As for the content, the content is actually the image. I uploaded the images into the db, the actual image and not a url. As for the loop, I understand all that. Assuming that is not case, I still cannot display the image using your new method, and using the old method, that code must be the only thing in the page or else it would not work. Allright, I may not have the GD installed. I downloaded the package from the website. Now how do install it? Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-336852 Share on other sites More sharing options...
ktalebian Posted August 29, 2007 Author Share Posted August 29, 2007 Ok, nvm. I installed GD. Now I get this error b>Warning</b>: imagegif(): supplied argument is not a valid Image resource in what does tha mean? Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-336879 Share on other sites More sharing options...
Fadion Posted August 29, 2007 Share Posted August 29, 2007 did u use smth like: $im = imagecreatefromgif('filename.gif'); imagegif($im); Im not sure when u say that the image is saved in the db. U should have images stored in a folder and save in db only the path or just the filename. Not sure how u stored them as images in db!! Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-336884 Share on other sites More sharing options...
ktalebian Posted August 29, 2007 Author Share Posted August 29, 2007 Yes that is the code that I have used. And yes, I uploaded the images using the following code: <?php // Gathering data from form $download_name = $_POST["download_name"]; $id = $_POST["id"]; $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; // Creating the $content file move_uploaded_file($_FILES['userfile']['tmp_name'],"latest.img"); $instr = fopen("latest.img","rb"); $content = addslashes(fread($instr,filesize("latest.img"))); // Connecting to db ... // Uploading file to db $query = "INSERT INTO upload (name, size, type, content, download_name, type_id ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$download_name', '$id')"; mysql_query($query); ?> the field "content" is a blob. Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-337034 Share on other sites More sharing options...
Fadion Posted August 30, 2007 Share Posted August 30, 2007 That is an interesting approach but id say pointless. Why should u upload an actual image to the database, when u first upload it? Just the upload should have been fine. Id suggest to modify your code and change the part where u write the image data to the db. Just write the imagefile ex: $fileName = $_FILES['userfile']['name']; $query = mysql_query("INSERT INTO upload (filename) VALUES ('$filename')"); //and all the other fields In this way u use the db to know what records have that image and show the actual image with a html img tags, gd or whatever u like. Believe me it will be a lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/66982-image-view/#findComment-337480 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.