PDA

View Full Version : File List Updating?


BlackHawk
01-06-2007, 11:43 PM
Hey guys, well I'm just starting to get into actually building websites instead of just designing them and so I've been fooling around with various coding. With the website (http://blackhawk.pyrom.net) Markpyro generously let's me use I have a static section for any general images / content I host for myself or others. To save me time in finding the exact file / url I made a page for the folder that holds all the files (http://blackhawk.pyrom.net/Static/).

The problem is that right now I have to update that list manually whenever I add a file. I was just wondering, is it possible (and if so, how I should go about) writing some code so that when I add something to the folder (site/Static/) it automatically add's an entry in the list for that file?

Modred
01-07-2007, 12:49 AM
If you program PHP, then you might want to check out PHP's Directory functions (http://us3.php.net/manual/en/ref.dir.php). With those, you should be able to open a directory, read in the file names, and then do whatever with them. It shouldn't be difficult to filter them by extension.

Now that I look some more, they even include an example of listing all files in a directory (http://us3.php.net/manual/en/function.readdir.php). You should be able to take this and make something to your liking.

Markpyro
01-07-2007, 12:13 PM
I did the same thing with my folder (http://pyrom.net/?p=temp),

If the PHP manual is a bit daunting at first glance, here's the script I used


<?php
echo "<b>The (not so) Temporary Folder</b>";
$path = "temp";
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
echo "<a href='$path/$file'>$file</a><br />";
}
closedir($dh);
?>


-MP


http://pyrom.net/sigs/manual/hr.png
http://pyrom.net/sigs/manual/sigimage.png
*http://pyrom.net/test/blamecountimage.php*http://pyrom.net/test/lastblameimage.php*
http://pyrom.net/test/blameclick.png (http://pyrom.net/test/blame.php)

BlackHawk
01-07-2007, 8:10 PM
If you program PHP, then you might want to check out PHP's Directory functions (http://us3.php.net/manual/en/ref.dir.php). With those, you should be able to open a directory, read in the file names, and then do whatever with them. It shouldn't be difficult to filter them by extension.

Now that I look some more, they even include an example of listing all files in a directory (http://us3.php.net/manual/en/function.readdir.php). You should be able to take this and make something to your liking.

Well the problem is I've never programmed in PHP before, so that could be useful in the future (I'm going to attempt to learn some PHP now, see paragraph after Markpyro's post). At this point though it doesn't help me much, though thanks for the links :).

I did the same thing with my folder (http://pyrom.net/?p=temp),

If the PHP manual is a bit daunting at first glance, here's the script I used


<?php
echo "<b>The (not so) Temporary Folder</b>";
$path = "temp";
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
echo "<a href='$path/$file'>$file</a><br />";
}
closedir($dh);
?>

-MP

Thanks, I'm going to use this script until I learn a little php myself and can figure out how to do it without stealing yours xD. My only question is, how do I incorporate that? Do I put it into the index.html file? Or is it a separate file that I call inside 'index.html' (similar to how Style's work)?

Thanks both of you for the help.

Markpyro
01-07-2007, 9:14 PM
First of all, you would rename your index to index.php. It'll be the same, but whenever my server hits the "<?php, ?>" tags, it'll parse the code within the tags instead of displaying it.

You would then copy the script into wherever you want the files in your directory to appear. I wont go further into this, as it should be self explanatory after a few tests (if not on the first try).

Just to let you know, within the operation echo, between the double quotes (") you cant use more double quotes, or it wont parse correctly. Use single quotes instead (').
Example

echo "<a href="hello">Hello</a> "; // You'll get an error
echo "<a href='hello'>Hello</a> "; // This should work


If you don't need to output any double quotes, then ignore everything I said previous.


http://pyrom.net/sigs/manual/hr.png
http://pyrom.net/sigs/manual/sigimage.png
*http://pyrom.net/test/blamecountimage.php*http://pyrom.net/test/lastblameimage.php*
http://pyrom.net/test/blameclick.png (http://pyrom.net/test/blame.php)

TimP
01-08-2007, 2:46 AM
You can use double quotes in double quotes as long as you escape them with a backslash.

echo "This is in \"double quotes\"";

Same thing goes with using single quotes in single quotes.

BlackHawk
01-08-2007, 3:24 PM
First of all, you would rename your index to index.php. It'll be the same, but whenever my server hits the "<?php, ?>" tags, it'll parse the code within the tags instead of displaying it.

You would then copy the script into wherever you want the files in your directory to appear. I wont go further into this, as it should be self explanatory after a few tests (if not on the first try).

Just to let you know, within the operation echo, between the double quotes (") you cant use more double quotes, or it wont parse correctly. Use single quotes instead (').
Example

echo "<a href="hello">Hello</a> "; // You'll get an error
echo "<a href='hello'>Hello</a> "; // This should work


If you don't need to output any double quotes, then ignore everything I said previous.

Right on, thanks. It's working right now (http://blackhawk.pyrom.net/Static/test/) (in a temp folder until I'm 100% sure about it, then I'll move it to Static/). One thing though, the first two entries in that ("." and "..") are, I'm assuming the "index.php" and "style.css" files in the /Static/test/ folder. However I made the path "content" so that it would grab only the files in "Static/test/content" (which I assume is how I should do it anyway) so I don't know why those two entries are there.

Here's the code incase you were wondering:

<?php
$path = "content";
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
echo "<li><a href='$path/$file'>$file</a></li>";
}
closedir($dh);
?>

I was thinking of adding to the while loop an if statement to prevent it from showing "index.php" and "style.css" (assuming that those are the "." and ".." entries). The if statement would be something like "if ($file != *.php or $file != *.css) then: " and then it would echo. I'm not sure if that would actually work but it's just what I was thinking :\.

Thanks again for your help.

Neo
01-08-2007, 3:30 PM
I've used this in the past. I really like it, clean, and purty. Same dude who made the upload script I use.

http://www.celerondude.com/php-indexer

-Kupa

TimP
01-08-2007, 3:41 PM
. is a symbolic folder meaning "the current folder" and .. means "the parent folder". In the while loop you can exclude ., .., and index.php.

Jeff
01-08-2007, 6:12 PM
Yeah just like example 2: http://us3.php.net/manual/en/function.readdir.php
Just add index.php to the list along with . and .. and you're good to go.

BlackHawk
01-08-2007, 7:39 PM
I've used this in the past. I really like it, clean, and purty. Same dude who made the upload script I use.

http://www.celerondude.com/php-indexer

-Kupa

Thanks, looking at his code definitely helped give me a little understanding of how to solve my problem.

. is a symbolic folder meaning "the current folder" and .. means "the parent folder". In the while loop you can exclude ., .., and index.php.

Ahh, I should've known that :>. Thanks though.

Yeah just like example 2: http://us3.php.net/manual/en/function.readdir.php
Just add index.php to the list along with . and .. and you're good to go.

Yeah I realized I should do that after looking at Neo's link (his code). (http://www.celerondude.com/php-indexer)

Well, it was working perfectly before (ignoring the ".", ".." and "index.php") but I was fidling around trying to add filesize (http://us3.php.net/manual/en/function.filesize.php) and for some reason my ftp doesn't want to let me connect so I can't update it to show you guys.

However here's the code I have at this point:

<?php
$path = "content";
$ignore=array('.','..','index.php','Thumbs.db');
$dh = opendir($path);

while (($file = readdir($dh)) !== false) {
if (in_array($file, $ignore) == false){
// $size = filesize($file); // To get the filesize of $file
// $type = strpbrk($file, '.'); // Was attempting to see if I could do something icon based to represent each file.
echo is_string($file)
echo "<li><a href='$path/$file'>$file</a> | $size | $type</li>";
// echo "<li><a href='$path/$file'>$file | $size</a></li>";
}
}
closedir($dh);
?>

My problem at this point is that I get an error when I use the filesize() function. Also, would the $file variable be a string? Because when I used the strpbrk() (http://ca.php.net/manual/en/function.strpbrk.php) function I got an error. Then again the error said that "strpbrk()" was an unknown function :\.

Modred
01-08-2007, 8:59 PM
strpbrk() is new to PHP5, so the most likely scenario is that the server has a version of PHP4. If you want to know the version, assuming MP isn't around to tell you right away, you can use the following code to find out:


<?php
echo "PHP version: ".phpversion();
?>


Right now, I just copied+pasted your code into my own server, running PHP5, and the filesize() function works. And yes, the file names are just strings. The strpbrk() also works, but as I stated earlier, that's because I'm using the newer version.

If you want to get the file extension, you could try something like this:


<?php
// strrpos(haystack,needle[,offset]) will find the last occurrence of the "needle" in the "haystack"
// substr(string,start[,end]) will return the string from start to end

// in this instance, we get the substring from the last occurrence of the period
// until the end of the filename
$type = substr($file,strrpos($file,'.'));
?>


One of the other guys here might have a better snippit of code to do that, but this works.

On another note, you might want to filter out directories using the is_dir() function, unless you want someone changing directories on you. Just a precaution, in case you ever make some subdirs in that folder.

Markpyro
01-08-2007, 9:23 PM
Edit- Yeh beat me to it, Modred :P



First of all, when you echo a PHP variable, you cant make it appear with in the quotes. See below:

<?php
$path = "temp";
$ignore=array('.','..','index.php','Thumbs.db');
$dh = opendir($path);

while (($file = readdir($dh)) !== false) {
if (in_array($file, $ignore) == false){
$size = filesize($file); // To get the filesize of $file
$type = strstr($file, '.'); // Was attempting to see if I could do something icon based to represent each file.
echo "<li><a href='" . $path . "/" . $file . "'>" . $file . "</a> | " . $size . "|" . $type . "</li>";
// echo "<li><a href='" . "$path . "/" . $file" . "'>" . $file . "|" . $size . "</a></li>";
}
}
closedir($dh);
?>

I couldnt get filesize to work, but the reason that strpbrk() wasnt working is that my host hasnt updated to PHP 5 yet, and that function is new to PHP 5, but I found a different function that outputs generally the same text. This code works except for filesize.



http://pyrom.net/sigs/manual/hr.png
http://pyrom.net/sigs/manual/sigimage.png
*http://pyrom.net/test/blamecountimage.php*http://pyrom.net/test/lastblameimage.php*
http://pyrom.net/test/blameclick.png (http://pyrom.net/test/blame.php)

Modred
01-08-2007, 9:40 PM
I've tested this on my server (PHP5) and from the command line (PHP4.4), and it works in both. I'm not sure why filesize() isn't working for you. Also, MP, the thing about concatentating the variables into the string was required in PHP3, but PHP4 and PHP5 allow you to simply embed the variable in the string as BH was doing. Note that I also added a linebreak, '\n', to the output so that your HTML output will look a little prettier to the human eye.


<?php
$path = ".";
$ignore=array('.','..','index.php','Thumbs.db');
$dh = opendir($path);

while (($file = readdir($dh)) !== false)
{
if (in_array($file, $ignore) == false)
{
$size = filesize($file); // To get the filesize of $file
$type = substr($file,strrpos($file,'.'));
// echo is_string($file);
echo "<li><a href='$path/$file'>$file</a> | $size b | $type</li>\n";

}
}
closedir($dh);
?>

BlackHawk
01-09-2007, 8:49 AM
Hah, well now it's working (http://blackhawk.pyrom.net/Static/test/), the reason filesize() wasn't working is that the files I was looking at were in another directory (the sub-directory "content") but when I made the path "." (for current directory as TimP pointed out and Modred had in his version of the code) then it worked fine.

Here's the code if you're interested:

<?php
$path = ".";
$ignore=array('.','..','index.php','style.css','Th umbs.db','arrow.png','cntnts-bg.jpg', 'floral-pattern.jpg');
$dh = opendir($path);

while (($file = readdir($dh)) !== false)
{
if (is_dir($file) == true)
{
$ignore[] = $file;
}
if (in_array($file, $ignore) == false)
{
$size = filesize($file);
$type = substr($file,strrpos($file,'.')); // strrpos(haystack,needle[,offset]) will find the last occurrence
// of the "needle" in the "haystack"
// substr(string,start[,end]) will return the string from start to end
echo "<li><a href='$path/$file'>$file</a> | $size b | $type</li>";
}
}
closedir($dh);
?>

After I move this completed version from Static/test/ to Static/ I'm going to try and make it so the size is converted to kb instead of b.

Thanks for all your help :). It's made me want to learn php now xD.