Jump to content

Reading Files in Directory


ParadoxKing

Recommended Posts

Okay, I fixed up the database script mentioned in my earlier topic. The script I originally made was for a web comic complete with an archive. Now however, I'm trying to write a similar script for my friend, he refuses to use the database version. So I'm using directory functions and the like. Before this I had a made a more simplistic script:

 

<?php
	$maximages = 5; //$maximages is set as an integer.
	if ($imagenum<=0||$imagenum>$maximages) {$imagenum=$maximages;}
	else {$imagenum=$imagenum;}
	echo sprintf("<img src=\"Comic%d.png\" name=\"comic\" /><br />",$imagenum);
	echo sprintf("<a href=\"index2.php?imagenum=%d\"><img border=\"0\" src=\"back.png\" alt=\"Previous Comic\" align=\"left\" /></a>",(($imagenum-1>0)?(--$imagenum)$imagenum)));
	echo sprintf("<a href=\"index2.php?imagenum=%d\"><img border=\"0\" src=\"next.png\" align=\"right\" alt=\"Next Comic\" />
		</a>",(($imagenum+1<=$maximages)?(++$imagenum)$imagenum)));
?>

 

The Comics were listed in the same directory as the index page and the $maximages var was defined as an integer. I soon grew very annoyed with this script because each time I uploaded a new comic I would also have to go into the code and change $maximages to whatever the new number of comics was. I started playing around with directories and failed miserably so I went out to the internet and got this (I editted the paths and changed the $count var to $maximages):

 

<?php
$dir = "comics/";
$maximages = 0;
if(is_dir($dir)) {
	if($handle = opendir($dir)) {
		while(($file = readdir($handle)) !==false) {
			$maximages++;
		}
	closedir($handle);
	}
}
echo $maximages;
?>

 

I moved the comics into their own subdirectory (comics) to simplify things. Then I echoed the $maximages var to see if the script worked. It didn't. The number of files it read totaled up to a big resounding 0. When I changed the value of $dir to ".", however, it returned the correct number of files. I rewrote the comic path several ways and still got nothing. Can anyone tell me why?

 

Also, when I used $dir = ".", the returned number included either hidden files or the files in the comics subdirectory. Is there away around that?

Link to comment
Share on other sites

Maybe I didn't use it right (I replaced $comics with $dir and substituted the existing $dir string with it), but I still gives me a big 0.

 

http://146.145.25.157/animeclub/nodbsample.php

 

See. The only possible reasons I can think of for this is that the path is written incorrectly and it can't find the directory, or it can see the directory, but can't read it.

Link to comment
Share on other sites

I would assume that is because you are setting $maximages = 0 then echoing $maximages, which is 0. glob() returns an array, try just this code:

$comics = glob("comics/*.jpg");
print_r($comics);

 

That should show you a list of all the comics. You can iterate through the array to use the files.

Link to comment
Share on other sites

No, that wasn't the problem. I feel really stupid now... I uploaded the page but I didn't upload the folder holding the comics. It returned 7, which means its still reading hidden files. Would your script help weed those out? Mind you all I want is the number of comics set as the $maximages var. I don't want to actually print them. The stuff with echo was just me testing the script.

Link to comment
Share on other sites

I simplified the script just using different methods of doing what you are trying to do and it may work

 

<?php
$dir = "comics/";
if(is_dir($dir)) {
	$maximages = 0;
	$files = glob($dir . '*');
	foreach($files as $file) {
		if(($file != '.') && ($file != '..')) {
			$maximages = $maximages+1;
		}
	}
}
echo $maximages;
?>

Link to comment
Share on other sites

Ok now try this one. I saw how you were getting the imagenum through get so i

went ahead and made sure it was a number to guard against SQL injections,

then i made the script as it will only show the next image if there is another

image and will only show the back image of you are not at the first image.

You can go in later and make a new image like your old one that show the

user that there is no more in that direction by adding an else statement.

 

The method you were using to determine the link for each image was over

complicated so i made it easier. I also check to see if there are any comics,

if there arent any you will get a message saying 'There are no comics'.

 

<?php
if(isset($_GET['imagenum'])) {
	if(is_numeric($_GET['imagenum'])) {
		$comics_num = $_GET['imagenum'];
		$comics_dir = "comics/";
		$comics_max = '0';
		if(is_dir($comics_dir)) {
			$comics_files = glob($comics_dir . '*');
			foreach($comics_files as $comics_file) {
				if(($comics_file != '.') && ($comics_file != '..')) {
					$comics_max = $comics_max+1;
				}
			}
		}
		if($comics_max > 0) {
			if($comics_num >= $comics_max) {
				$comics_num = $comics_max;
			}
			if($comics_num > 0) {
				print '<a href="index2.php?imagenum=' . $comics_num-1 . '">';
				print '<img border="0" alt="Previous Comic" title="Previous Comic" align="left" src="back.png" />'
				print '</a>';
			}
			print '<img src="Comic' . $comics_num . '.png" name="comic" /><br />';
			if($comics_num < $comics_max) {
				print '<a href="index2.php?imagenum=' . $comics_num+1 . '">';
				print '<img border="0" alt="Next Comic" title="Next Comic" align="right" src="next.png" />';
				print '</a>';
			}
		}
		else {
			print 'There are no comics';
		}
	}
	else {
		print 'Bad input';
	}
}
?>

Link to comment
Share on other sites

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.