Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/180027-using-while-on-readfile/
Share on other sites

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().

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?

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.

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.