Gamerz Posted November 2, 2009 Share Posted November 2, 2009 Hello, I am trying to use the: while($row = mysql_fetch_array( $result )) { and inside the while, I want to read every image on the mysql table using readfile(); Alright, so my problem is that after I use readfile(); the script just stops running. How do I know? Well, I tested it by echoing something right after readfile():, and it did not display...so how do I make it so it finds every image and displays it...not just displays one file and the script stops.... So here's a snippet of my code: <?php // Connect to DB require_once('include/configure.php'); // Connect to the server $db->connect(); // Root Dir. $fulldir = "/root/d74156a/home/docs/files/"; $file_name = $fulldir.$row['file_name']; $ext = strtolower(substr(strrchr($file_name,'.'),1)); // Below are just mime type settings. I deleted it because it isn't revelant to what I'm going to ask you. $result = mysql_query("SELECT * FROM allfiles"); // Loop through the files while($row = mysql_fetch_array($result)) { // This checks if the file name's are all images. If they are not images, cancel out the non-image file to prevent it from being displayed. if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file_name)) { readfile($file_name); echo "TESTTEST<hr><br>fgdfgsdg"; } } ?> Currently with the above script, the script displays one image (the first row on mysql), then the script just stops...any reason why? As you can see, I made an echo; but it does not echo out the above on my browser... Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/ Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Maybe it had a heart attack ? try adding more memory to the computer.. (a LOT more) What are you even trying to do ? other than kill apache! Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949765 Share on other sites More sharing options...
Gamerz Posted November 2, 2009 Author Share Posted November 2, 2009 LOL no seriously... Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949766 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 above post was updated: what are you trying to do.. as your likely using a bucket load of memory with zero gain! Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949768 Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 So you want to show all images returned? readfile() won't be the way to go then. readfile() puts the data directly to the output buffer, so you can't output multiple files images. In your case I think you want to do something like this: <?php // Connect to DB require_once('include/configure.php'); // Connect to the server $db->connect(); // Root Dir. $fulldir = "/root/d74156a/home/docs/files/"; $file_name = $fulldir.$row['file_name']; $ext = strtolower(substr(strrchr($file_name,'.'),1)); // Below are just mime type settings. I deleted it because it isn't revelant to what I'm going to ask you. $result = mysql_query("SELECT * FROM allfiles"); // Loop through the files while($row = mysql_fetch_array($result)) { // This checks if the file name's are all images. If they are not images, cancel out the non-image file to prevent it from being displayed. if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file_name)) { echo "<img src='$file_name' alt='' />"; } } ?> It's also not a good idea to use ereg because it's depreciated and removed as of PHP6, a better alternative would be preg_match(). Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949769 Share on other sites More sharing options...
Gamerz Posted November 2, 2009 Author Share Posted November 2, 2009 That doesn't work...it only display's three <hr's>... Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949771 Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 <hr>s? How is that even possible.. Is there other code that you're not showing us, and are you sure that $file_name contains the data you're expecting? Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949773 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 what are you trying to do.. As your code only loads the same image over and over.. it makes little sense! Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949776 Share on other sites More sharing options...
Gamerz Posted November 2, 2009 Author Share Posted November 2, 2009 Nope; everythings on there...it literally display's the <hr>, like the lines....and theres three of them; which makes sense; because there are three images on the table; whats weird is that since it knows there are three images why isn't it showing the images? Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949777 Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 what are you trying to do.. As your code only loads the same image over and over.. it makes little sense! Lmao, I didn't even realize that.. I should actually take 5 seconds and look at the code. But yea, you're not going to want to use readfile().. If you're trying to output an image using that (assuming you have the right Content-type header set) only the first image output will actually be displayed. I'm not completely sure, but I'm assuming this is what you want. <?php // Connect to DB require_once('include/configure.php'); // Connect to the server $db->connect(); // Root Dir. $fulldir = "/root/d74156a/home/docs/files/"; $ext = strtolower(substr(strrchr($file_name,'.'),1)); // Below are just mime type settings. I deleted it because it isn't revelant to what I'm going to ask you. $result = mysql_query("SELECT * FROM allfiles"); // Loop through the files while($row = mysql_fetch_array($result)) { // This checks if the file name's are all images. If they are not images, cancel out the non-image file to prevent it from being displayed. if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $row['file_name'])) { $file_name = $fulldir.$row['file_name']; echo "<img src='$file_name' alt='' />"; } } ?> Notice that defining of $file_name is within the loop, this is because it needs to be altered with every iteration. Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949780 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 Oh and as stated by AlexWD, ereg it's depreciated, but you could also do this change if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $row['file_name'])) { to if(in_array(strtolower(substr($file_name, strrpos($file_name, '.')+1)),array("jpg","jpeg","bmp","png","gif"))){ EDIT: oops forgot about uppercase (updated) Quote Link to comment https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/#findComment-949784 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.