dbradbury Posted February 9, 2010 Share Posted February 9, 2010 subject really asked my question lol <?php if(isset($_GET['player'])) { $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'"); $countid = mysql_num_rows($idpic); if($countid!=0) { $row = mysql_fetch_assoc($idpic); $file = './forum/files/'.$row['physical_filename']; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } } } ?> at the moment it is asking me to download the file... Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/ Share on other sites More sharing options...
bbaker Posted February 9, 2010 Share Posted February 9, 2010 you code is forcing a download....to display an image, just use an img tag....no need to us readfile at all Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009638 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 ive tried this, but it wont do it, as when the image is stored, its extension is taken off, so it just becomes a file :\ but ive just thought of something.... Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009646 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 nah didnt work, i was trying.. <?php if(isset($_GET['player'])) { $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'"); $countid = mysql_num_rows($idpic); if($countid!=0) { $row = mysql_fetch_assoc($idpic); $file = './forum/files/'.$row['physical_filename']; if (file_exists($file)) { header('Content-Type: image/jpg'); echo '<img src='.$file.''; } } } ?> but nothing.. Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009648 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 Hi dbradury, Your modified code is nearly there. Try this: <?php if(isset($_GET['player'])) { $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'"); $countid = mysql_num_rows($idpic); if($countid!=0) { $row = mysql_fetch_assoc($idpic); $file = './forum/files/'.$row['physical_filename']; if (file_exists($file)) { $data = readfile($file); header('Content-Type: image/jpg'); echo $file; } } } ?> I haven't checked it but it will hopefully send you in the right direction even if it doesn't work. Fergal Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009654 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 Hi Fergal, thanks for the help btw.. i place the code in and uploaded it, the i viewed the page and i got a blank screen, well white background and word image in the centre, with like a box round it.. but i took out header ('Content Type: image/jpg'); and view that page again and got this... was weird... ÿØÿá2äExif and aload of other stuff like that... also it showed my phone make and model, date and time of the picture, when it was taken :\ lol Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009666 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 Hi dbradbury, I should have spotted it earlier but 'image/jpg' should be 'image/jpeg'. Try changing that. If that doesn't work are you sure it's a jpg? If not try image/x-png and image/gif. Fergal Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009688 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 no such luck, it did the same thing everytime i know it is a jpg, as i saw the extension when i uploaded it but this is where the pictures are displayed.. and as you can see there is no extension on any of the files.. thats because the forum, removes the extension :\ i think Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009691 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 hmmm.... I wonder if they are compressing the data or something as well as removing the extension. Can you try your script on a file on your local machine? Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009703 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 how do you mean? uploading an image into that directory? Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009704 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 point readfile() at an image on your computer and see if it works with that. e.g. $file = /home/images/test.jpg; readfile($file) Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009711 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 ahh lol and nope, it did the same thing... Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009736 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 Sorry, I should have spotted the schoolboy error in the modifed script I gave you (it's been a long day!). It was echoing the path to the file and not the data. Try this on your local file and then the remote one. <?php if(isset($_GET['player'])) { $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'"); $countid = mysql_num_rows($idpic); if($countid!=0) { $row = mysql_fetch_assoc($idpic); $file = './forum/files/'.$row['physical_filename']; if (file_exists($file)) { $data = readfile($file); header('Content-Type: image/jpg'); echo $data; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009748 Share on other sites More sharing options...
dbradbury Posted February 9, 2010 Author Share Posted February 9, 2010 nothing again sorry about this, its such a pain, but i appreciate the help you have given me, thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009759 Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 sorry d, my tiredness is getting in the way this evening. Try changing 'image/jpg' to 'image/jpeg' Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009776 Share on other sites More sharing options...
dbradbury Posted February 10, 2010 Author Share Posted February 10, 2010 nope ill come back to it in the morning, as im tired lol Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1009792 Share on other sites More sharing options...
dbradbury Posted February 10, 2010 Author Share Posted February 10, 2010 anyone got a clue about this? Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1010060 Share on other sites More sharing options...
dbradbury Posted February 16, 2010 Author Share Posted February 16, 2010 all done, i used this slightly.. but the picture show perfect now... Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1013353 Share on other sites More sharing options...
Fergal Andrews Posted February 16, 2010 Share Posted February 16, 2010 Hi dbradbury, sorry for not following this up but I've been really busy lately. How did you sort it and well done for doing so? Fergal Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1013374 Share on other sites More sharing options...
dbradbury Posted February 16, 2010 Author Share Posted February 16, 2010 i had to use a script on another page... main page code: <?php if(isset($_GET['player'])) { $idpic = mysql_query("SELECT * FROM phpbb_attachments WHERE post_msg_id='$player'"); $countid = mysql_num_rows($idpic); if($countid==1) { $row = mysql_fetch_assoc($idpic); if ($row['thumbnail'] =0) { print '<img src=loadimage.php?image='.$row['physical_filename'].'>'; } else { print '<img src=loadimage.php?image=thumb_'.$row['physical_filename'].'>'; } } else { echo 'No image yet'; } } ?> and my script page: <?php $image = $_GET['image']; header('Content-Type: image/jpeg'); readfile('forum/files/'.$image); ?> and it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/191526-how-do-you-use-readfile-to-display-a-picture/#findComment-1013387 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.