readdir followed by is_file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

readdir followed by is_file

Post by db579 »

I'm trying to make use of some code for a simple photo gallery but I'm running into problems because the photos are no longer all stored in the same directory as the function script.
The script seems to run into problems at line 42 as the script is no longer stored in the same directory as the images. Full script is attached but this is the section I think I'm having problems with. Thanks very much for any help.

Code: Select all

function generateThumbnails(){
	global $thmb_width,$thmb_height,$dir;

	// Open the album directory
	if ($handle = opendir($dir)) {
		// Read all files from the album directory
		while (! == ($file = readdir($handle)))  {
			// Check whether the item is a valid file
			if (is_file($file)){
				// Check whether the image is a thumbnail
	      		if (strpos($file,'_th.jpg')){
	      			$isThumb = true;
	      		} else {
		      		$isThumb = false;
		      	}
Last edited by db579 on Sun Jul 18, 2010 7:23 pm, edited 1 time in total.
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: readdir followed by is_file

Post by Gargoyle »

you should really properly format your code if you are asking for help. also, mentioning "line 42" is rather pointless here.

other than that, you just need to supply the directory path by adding

Code: Select all

$file = $dir.$file;
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: readdir followed by is_file

Post by db579 »

Hi thanks for the help and sorry about the formatting. I've tried adding that into the while loop like so:

Code: Select all


while (! == ($file = readdir($handle))) {
$file =$dir.$file;
// Check whether the item is a valid file
if (is_file($file)){

But it still doesn't work. This part does work if I add $dir.$file into the is_file statement itself instead though which is why I'm very confused by this but doing it that way is causing me problems further on. What am I doing wrong? Thanks again
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: readdir followed by is_file

Post by Gargoyle »

if you want to address a file that is not located in the same folder than the php script executed, you need to specify the folder as well. if this causes you further troubles, you need to revise your code.
Post Reply