strazdinjsh Posted November 15, 2010 Share Posted November 15, 2010 That must be very simple but i can not step over the issue what i have got. 1. i have file which displays the image called index.php with source code of -------------------------------------------------------------------- ... <img src="sample.php?id=42" /> ... -------------------------------------------------------------------- 2. then i have file sample.php with source code ------------------------------------------------------------------------------------- require("sys/functions.php"); dbConnect(); $id = $_GET["id"]; if(!isset($id)) { echo "select an ID"; } else { $res = mysql_query("SELECT * FROM gallery where id=$id"); $row = mysql_fetch_assoc($res); header("Content-type: image/$row[extension]"); echo $row['image']; } ------------------------------------------------------------------------------------------- 3. i have successfully uploaded file to MySQL, i can see it as a record with id=42 (image - that is the actual file, extension - image extension) 4. when launching the index.php i am not getting an image but still can get a file size, approx. must be right 248kb. image itself does not display Could anyone have a look and give me some idea to start with... thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/ Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 Is this issue too complicated or too simple to try to solve or give the advice? Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134354 Share on other sites More sharing options...
revraz Posted November 15, 2010 Share Posted November 15, 2010 Since you only gave it a couple of hours, try being a bit more patient. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134374 Share on other sites More sharing options...
harristweed Posted November 15, 2010 Share Posted November 15, 2010 can you have an image with a .php extension? Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134384 Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 No, image does not show up even with .php Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134386 Share on other sites More sharing options...
litebearer Posted November 15, 2010 Share Posted November 15, 2010 Does the database field contain the image name and location? simple image display is <IMG src="gallery/flower.jpg"> presuming database field for is named image , using php you would say ?> <IMG src="<?PHP echo $row['image']; ?>"> <?PHP Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134389 Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 DB field does not contain name and the path to the image because image is located in DB itself. It is not the name of file what is kept in table but image. Is that an answer on your question? if i am wrong, correct me please. P.S. I used to work with mysql and usually kept the path to the image, there were no problems. I am trying to keep images in DB instead of web root folders Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134395 Share on other sites More sharing options...
PFMaBiSmAd Posted November 15, 2010 Share Posted November 15, 2010 What exactly does $row[extension] contain, because there are only a few Content-type: image/ values where the actual file extension IS a valid Content-type? What do you get when you browse directly to sample.php?id=42 ??? If you get a blank screen when you do that, what does a 'view source' in the browser show? Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed? Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134396 Share on other sites More sharing options...
litebearer Posted November 15, 2010 Share Posted November 15, 2010 a tutorial on displaying images saved as blobs in the database http://forum.codecall.net/php-tutorials/7663-tutorial-storing-images-mysql-php-part-ii-display-your-images.html Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134408 Share on other sites More sharing options...
PFMaBiSmAd Posted November 15, 2010 Share Posted November 15, 2010 The code that has been posted is valid (with some assumptions and crossed fingers), so it is a matter of troubleshooting what it actually is doing or not doing. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134411 Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 $row[extension] holds the extension of the file and in this case is jpg, it is necessary to define what file output will be expected - picture (png, gif, jpeg, jpg etc.) or pdf or... I am getting image approx 50 x 50 which usually showed when image can not be found or just output. View Source is disabled, i can not look inside the source generated by browser. Opera lets check the file size - approx 259KB. Yes, E_ALL is set and display errors is ON in php.ini Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134412 Share on other sites More sharing options...
PFMaBiSmAd Posted November 15, 2010 Share Posted November 15, 2010 While it might not be the only problem preventing your image from working, as already stated, the content type for a .jpg is not image/jpg, it's image/jpeg Edit: If your browser indicates the size of the output is the expected value, it is likely that using the correct Content-type: will fix the problem. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134416 Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 unfortunately i have tried the extension jpeg also without success, i have replaced $row[extension] with jpg and jpeg without possibility fail because of mysql or query etc. would my php code work on other servers, might be server settings or smth? Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134430 Share on other sites More sharing options...
PFMaBiSmAd Posted November 15, 2010 Share Posted November 15, 2010 Temporarily comment out the header() statement and add the following two lines right after your first opening <?php tag in sample.php - ini_set("display_errors", "1"); error_reporting(E_ALL); I also recommend that you post the code that uploaded and inserted the file into the database. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134440 Share on other sites More sharing options...
strazdinjsh Posted November 15, 2010 Author Share Posted November 15, 2010 Thank you for comments, i will try to do your suggestions, have to leave - i am lucky because working hours are over . Hope it works, otherwise see you tomorrow. have a nice day Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134444 Share on other sites More sharing options...
BlueSkyIS Posted November 15, 2010 Share Posted November 15, 2010 P.S. I used to work with mysql and usually kept the path to the image, there were no problems. I am trying to keep images in DB instead of web root folders There are more options than these 2. I can't think of any good reason to keep binary files in a database. But... I have been wrong before. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134450 Share on other sites More sharing options...
strazdinjsh Posted November 16, 2010 Author Share Posted November 16, 2010 I have tried to put some lines of code previously advised (put in comments header() also) without success. errors does not show up any errors. Have anyone tried to put these lines of code on their server, did it work? Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134885 Share on other sites More sharing options...
PFMaBiSmAd Posted November 16, 2010 Share Posted November 16, 2010 With the following code in sample.php, what do you get when you browse directly to sample.php?id=42 <?php ini_set("display_errors", "1"); error_reporting(E_ALL); require("sys/functions.php"); dbConnect(); $id = $_GET["id"]; if(!isset($id)) { echo "select an ID"; } else { $res = mysql_query("SELECT * FROM gallery where id=$id"); $row = mysql_fetch_assoc($res); // header("Content-type: image/$row[extension]"); echo $row['image']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134886 Share on other sites More sharing options...
strazdinjsh Posted November 16, 2010 Author Share Posted November 16, 2010 sorry, i did not reply on your previous comment correctly. I did not get any result with your suggestions when launched index.php (source code showed ..... <img src="sample.php?id=42" />.......). When i launched sample.php?id=42 i got something like binary code of the picture output. Unfortunately i could not get my machine with apache on it at work, so i can not double check it. I will get back as soon as i will be able to. Thank you for your time and efforts. P.S. i will try to use other record with picture, might be problem is hiding in the file or extension or.. or.. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1134891 Share on other sites More sharing options...
strazdinjsh Posted November 16, 2010 Author Share Posted November 16, 2010 i did double check, it worked as described in previous comment by me. shows binary data of file when sample.php?id=42 launched and does not show anything when index.php launched + source code says: <img src="test.php?id=42" /> P.S. Still looking for help. thank you Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135097 Share on other sites More sharing options...
litebearer Posted November 16, 2010 Share Posted November 16, 2010 quoting PFMa I also recommend that you post the code that uploaded and inserted the file into the database. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135099 Share on other sites More sharing options...
strazdinjsh Posted November 16, 2010 Author Share Posted November 16, 2010 image insertion below (DB table gallery with fields - id(int, auto increment), image(longblob), extension(varchar), date_time(timestamp)) require("sys/functions.php"); dbConnect(); $data = file_get_contents("img/test.jpg"); $data = mysql_real_escape_string($data); mysql_query("INSERT INTO gallery SET image='$data'"); image output below require("sys/functions.php"); dbConnect(); $id = $_GET["id"]; if(!isset($id)) { echo "select an ID"; } else { $res = mysql_query("SELECT * FROM gallery where id=$id"); $row = mysql_fetch_assoc($res); $im = $row["image"]; header("Content-type: image/jpeg"); echo $im; } Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135114 Share on other sites More sharing options...
strazdinjsh Posted November 17, 2010 Author Share Posted November 17, 2010 I apologize for the insertion code - there is an extra field [extension] in table who was added manually afterwords when dealing with issue. Field info added manually as [jpg] and tried [jpeg] and meant to use for content output. In output source code i removed content output detection and set it manually to [jpeg] Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135407 Share on other sites More sharing options...
strazdinjsh Posted November 18, 2010 Author Share Posted November 18, 2010 Is there anything wrong with my code above or any other suggestions will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135953 Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2010 Share Posted November 18, 2010 There are at least 3 things that could prevent what you are doing from working - 1) Something you have in your image output code that you have not shown in the post, such as using a short open tag or intentionally echoing something before the code you have shown, is preventing the code from being seen as code or is preventing the content-type header from working. Could you post the WHOLE contents of that file? Your "sys/functions.php" file or your dbConnect(); function could also be causing some inadvertent output to the browser that is preventing the content-type header from working. 2) Your image output code file could have the Byte Order Mark (BOM) characters at the start of the file (put there by your editor when the file is saved for a UTF-8 encoded file) and the content type header is not working. The display_errors/error_reporting settings that have been suggested is something you should always have set as suggested when developing and DEBUGGING php code and would point out if there are any problems that php can detect that would help solve the problem. Having the display_errors/error_reporting settings set as suggested would also help find if there is any other output, like mentioned in item #1 above, that is preventing the content-type header from working. 3) magic_quotes_runtime could be on which would cause your image data to be double escaped and invalid when you retrieve it and output it. What does the following php code show for the magic_quotes_runtime setting - <?php var_dump(get_magic_quotes_runtime()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218701-image-retrieving-from-mysql-problem/#findComment-1135985 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.