Warning: fopen(graphic_design/files/thread-1267-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421 file NOT opened
php directory index -
ok, here is a php script that reads the contents of a directory, and generates a directory listing.
i got the original script from http://evoluted.net/archives/000031.php then i tweaked it a bit. some cosmetic changes, but a pretty significant change was adding an Exclude array so that you can specify files/folders that you don't want included in directory listing. also changed and added file types recognized.
another thing that i added was a floating preview window for pictures. it uses imagemagick to resize. this needs a little work though. it breaks for non-images, so that's something that needs to be looked into. (any advice on this would be good).
the resize file only needs to be in the root directory, and the index file needs to be in every directory (obviously). i use the *nix ln command to make links of the index file, so that i only have to make changes once, instead of for every index file in every directory. although if your server doesn't use *nix, then you can't use this method.
and see it in action
http://turtle.komaru.org/
and if you want the files
http://glyphon.spymac.net/directoryindex.zip
Koobi
07-09-2004, 08:41 AM
Reall good script :) That floating preview is a definite plus :)
About it breaking on non images, can't you check for the MIME and then give it the appropriate function?
I created something like this a few weeks back too:
http://koobi-studio.com/downloads/php/classes/KdirLister/
Made it from scratch and it's OOP :)
Glyphon
07-09-2004, 09:12 AM
oh. i like how the folders indicate the number files inside, and the size. nice touch. as is the path being broken down into into links for the individual directories.
i never thought of the numbers for the folders, but i did want to have the path like you have yours set up. but since i'm still kinda learning php (slowly), i wasn't sure how to go about that. and after this weekend, i'll look into the mime check.
Nemesis
07-09-2004, 06:21 PM
Both of those scripts are awesome.
I also liked that image preview pop-up :grin2:
Koobi
07-10-2004, 02:55 AM
Thanks nemesis :D
oh. i like how the folders indicate the number files inside, and the size. nice touch. as is the path being broken down into into links for the individual directories.
I just use is_dir (http://www.php.net/is_dir) (while reading the files in) to check if the current file is a dir and if it is, I read through that.
The best way to do it would be to have a function that reads the dir and returns the files.
i never thought of the numbers for the folders, but i did want to have the path like you have yours set up. but since i'm still kinda learning php (slowly), i wasn't sure how to go about that. and after this weekend, i'll look into the mime check.
In my script, the first 20 lines or so of the "assemble" method is the one that handles the "breadcrumb" navigation :)
About the MIME thing, I will have a look at it to. I generally have an array with accepted image extensions and get the files extension and compare but I'm sure there's a more efficient method but I only started PHP in January this year :D
:edit:
Sorted the MIME issue :)
I haven't tested it yet though. Just wrote it :)
The problem here is that this requires PHP 4.3.0 at least because of mime_content_type (http://php.net/mime_content_type)
You can insert this before this line in your script:
if(filetype($file) == "dir")
<?php
//mime_content_type returns "image/FOO" where FOO is the image extension/type
$fileMime = mime_content_type($file);
//Check if the first 5 chars are "image"
if(substr($fileMime, 0, 5) == 'image')
{
//Perform image functions here
}
?>
Glyphon
07-12-2004, 11:33 PM
hmm...that didn't work. i'm using php 4.3.4, and according to phpinfo() mime_magic is enabled, but when i tried that code, it spit out a mime_magic error (mime_magic could not be initialized, magic file is not avaliable in PATH.
so i had to figure out a way around that. and this is how i did it.
$AutorisedImageType = array ("jpg", "jpeg", "gif", "png");
$images = $files[$i];
$ext = substr($images, strpos($images, ".")+1);
if( in_array($ext, $AutorisedImageType) )
{
//floating code for matching exts
} else {
//code for rest exts
}
next...breadcrumbs.
anybody think or something else that would be a nifty feature to add?
Koobi
07-20-2004, 02:19 AM
heh heh nice one :)
I wrote one to check for extensions a while back and it was terribly inefficient compared to yours :D Nice job :D
But the problem is, if your image is named: "image.client.preview.jpg", this would not work though it's a valid file name according to the specifications, so I created this:
This function will accept either a file name only or the file name plus the relative path to it but it only accepts strings (since it's only built to manipulate strings). If anything but a string is passed into it as the file name, the function will return FALSE.
The function will return an associative array; the key 'type' will hold the extension of the file and the key 'name' will hold the full name of the file (even if the file name has multiple periods in its name)
The OPTIONAL_PADDING_CHARACTER will replace any periods in the file name. This parameter is optional as you can see, but by default, that parameter is set to be a blank space.
NOTE: I used a for loop instead of a iterating with foreach because an additional if statement would be necessary (to know when to break the iteration so that the extension is not added as a part of the name).
----- script came here -----
Here are a few examples:
<?php
$file = fullFileName('file.class.extender.php', '>');
echo 'Type: ' . $file['type'] . '<br />Name: ' . $file['name'];
/*
*
* The above code will yield:
* Type: php
* Name: file>class>extender
*
*/
There's only one problem with this, if you're a linux user who uses "tar.gz" files a lot, this won't show you the right output :)
Example:
If the filename is "myarchive.tar.gz", the correct extension should be tar.gz but my script will only show "gz".
To fix this, you can define all double extension file names in an array and use "in_array" to sort the problem out. I didn't include it in the script since it was only meant for images which have only one extension as far as I know :)
But the problem is, if your image is named: "image.client.preview.jpg", this would not work though it's a valid file name according to the specifications...
oh man, you are right!!! nice catch! i completely missed that. :grin2:
Thanks to strrchr (http://www.php.net/strrchr).
yup! that worked great!!! thanks for the tip!!! :grinning:
Leech
08-02-2004, 12:38 PM
banes script is nice, can also be made to fit with my sites theme, woohoo ! yay bane !
Koobi
08-08-2004, 04:01 PM
oh man, you are right!!! nice catch! i completely missed that. :grin2:
yup! that worked great!!! thanks for the tip!!! :grinning:
Actually I only realized that after I went through your snippet so thank you :D
@Leech: and you didn't even break it heh heh
I'm going to rewqrite the code to my current directory lister to make it easier to use and to add some features...I would appreciate some suggestions if any of you have any :)