wrathican Posted August 15, 2007 Share Posted August 15, 2007 hey guys im trying to list files in a directory, the directory contains many different types of files like images, pdf's, sub directories, txt files and other php files. what i wanna do is list the files with links to each of them, but with images i want a little thumbnail to show up next to it. so far ive got this: <?php if ($handle = opendir('.')) { echo "<ul>"; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $ext = strstr($file, '.'); if($ext == ".gif"){ echo "<li><a href='".$file."'><img src='".$file."' width='25'>".$file."</a></li>\n"; }else{ echo "<li><a href='".$file."'>$file</a></li>\n"; } } } closedir($handle); echo "</ul>"; } ?> how do i get it so that my if statement can take multiple examples of $ext? like .jpg, .png and such other image files? when i load the file i don't get the little thumbnail i 'think' ive made also my brief says this too: Thumbnails would be one great way of quickly seeing the contents of images, text files etc is it possible to show a thumbnail of inside a txt file? :S thanks alot Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted August 15, 2007 Share Posted August 15, 2007 Try: $array = array('.gif','.jpg','.jpeg','.png'); if($ext == $array){ ... Quote Link to comment Share on other sites More sharing options...
chocopi Posted August 15, 2007 Share Posted August 15, 2007 wouldn't it be easier to user glob ??? <?php echo "<ul>"; $array = array('gif','jpg','jpeg','png'); for($i = 0;$i <= 3; $i++) { foreach (glob("*.{$array[$i]}") as $filename) { echo "<li><a href=\"{$filename}\">"; echo "<img src=\"{$file}\"' width=\"25\" border=\"0\">"; echo "{$filename}</a></li>\n"; } } echo "</ul>"; ?> ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 Try this (worked for me): <?php echo "<ul>"; $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); $is_img = (in_array($ext, $img_extensions)) ? 1 : 0; echo "<li><a href='".$file."'>"; if($is_img) echo "<img src='".$file."' width='25' border='0'>"; echo $file."</a></li>\n"; } } echo "</ul>"; ?> also my brief says this too: Quote Thumbnails would be one great way of quickly seeing the contents of images, text files etc is it possible to show a thumbnail of inside a txt file? :S I guess you could read the contents of the file into a string and write it into a picture using the GD library but I dont think its worth it... Orio. Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted August 15, 2007 Share Posted August 15, 2007 When it comes to previews, I use orangoo's greybox. I can do this with images, not really tried a plain text file, but might be possible, html pages will render, so plain text should not be hard. When you are checking the file extensions you probably will need to build a switch/case so the right type of link is built for greybox. Just a thought...certainly not necessary. http://orangoo.com/labs/GreyBox/ Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 thanks for your help guys. ive managed to come up with this: <?php $ext_list = array ('.jpg', '.JPG', '.gif', '.GIF', '.png', '.PNG', '.JPEG', '.jpeg'); $list_ignore = array ('.','..'); if($handle = opendir('.')){ echo "Directories:<ul>"; while($dir = readdir($handle)) { if (is_dir($dir) && !in_array($dir,$list_ignore)) { echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>'; } } echo "</ul>Images:<ul>"; while($image = readdir($handle)) { $ext = strstr($image, '.'); if(in_array($ext,$ext_list)){ echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n'; } } } ?> but i cant get it to list the images in the directory :/ any ideas why? Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted August 15, 2007 Share Posted August 15, 2007 Try the ext variable as the haystack. if(in_array($ext_list,$ext)){ echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n'; } --Edit Also try: <?php $ext_list = array ('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG', 'JPEG', 'jpeg'); $list_ignore = array ('.','..'); if($handle = opendir('.')){ echo "Directories:<ul>"; while($dir = readdir($handle)) { if (is_dir($dir) && !in_array($dir,$list_ignore)) { echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>'; } } echo "</ul>Images:<ul>"; while($image = readdir($handle)) { $ext = strstr($image, '.'); if(in_array($ext,'.'.$ext_list)){ echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n'; } } } ?> Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 nope, its still the same. thanks for the help anyways! Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 Try this (worked for me): <?php echo "<ul>"; $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); $is_img = (in_array($ext, $img_extensions)) ? 1 : 0; echo "<li><a href='".$file."'>"; if($is_img) echo "<img src='".$file."' width='25' border='0'>"; echo $file."</a></li>\n"; } } echo "</ul>"; ?> also my brief says this too: Quote Thumbnails would be one great way of quickly seeing the contents of images, text files etc is it possible to show a thumbnail of inside a txt file? :S I guess you could read the contents of the file into a string and write it into a picture using the GD library but I dont think its worth it... Orio. I already told you the script above, that was made by me, works exactly like you want it, so why are you ignoring it? Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 im sorry, im not ignoring it. in fact im using yours as a reference as to why mine wont work. your script does not do exactly what i want though. Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 What do you want? Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 well, i want each type of file to list under a seperate heading. like this: Directories: -D1 -D2 Images: -I1 -I2 Other files: -O1 -O2 something like that at the moment my directories works but no images display under the images header. and i dont get why, it looks like it should be able to do it when i look at it, but i mimght be missing something: $ext_list = array ('.jpg', '.JPG', '.gif', '.GIF', '.png', '.PNG', '.JPEG', '.jpeg'); $list_ignore = array ('.','..'); $handle = opendir("."); if($handle = opendir('.')){ //lists directories echo "Directories:<ul>"; while($dir = readdir($handle)) { if (is_dir($dir) && !in_array($dir,$list_ignore)) { echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>'; } } //lists images echo "</ul>Images:<ul>"; while($image = readdir($handle)) { $ext = strstr($image, '.'); if(in_array($ext,$ext_list)){ echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n'; } } //lists all other files echo "</ul>Other files:<ul>"; while($other = readdir($handle)) { $ext = strstr($other, '.'); if(!in_array($ext,$ext_list) && !is_dir($other)){ echo '<li><a href="'.$other.'">'.$other.'</a></li>\n'; } } } Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 <?php $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><br>\n<ul>"; foreach ($dirs as $file) echo "<li><a href='".$file."'>".$file."</a></li>\n"; echo "</ul>\n\n<br><br>\n\n"; } if(count($imgs) > 0) { echo "<b>Images</b><br>\n<ul>"; foreach ($imgs as $file) echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'>".$file."</a></li>\n"; echo "</ul>\n\n<br><br>\n\n"; } if(count($others) > 0) { echo "<b>Other Files</b><br>\n<ul>"; foreach ($others as $file) echo "<li><a href='".$file."'>".$file."</a></li>\n"; echo "</ul>"; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 thats exactly what i wanted. I feel cheeky using someone else's code though. would you be able to guide me through any errors in my script at all please? any ideas as to why it wont list anything but the directries? Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 Your script isn't listing the other stuff because you need to reopen the dir in each one of the three loops, because if you continue and use the same resource from the first loop its internal pointer will already be in the end, meaning you will got false right away. close the dir and reopen it, then it should work (unless you got some other error). I think its much more efficient in my way- going over the files only once. But thats up to you- if you want to go over the entire directory 3 times, enjoy. Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 thanks alot for the help. now you have mentioned it i can see what the problem is! the main reason i wanted to use mine is that i understand exactly what is going on in it. but what im going to do is sit down and study yours just to make sure i do understand it. thanks again! Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 No problem Enjoy. Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 just a quick question. what is this bit doing? i think i understand the rest: <?php foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); //mainly wondering what this bit is if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted August 15, 2007 Share Posted August 15, 2007 For every file, it gets the extension in $ext, and then checks- *If the file is a dir, it puts it in the dirs array *If the file is a image, it puts it in the images array *Everything else, into the "others" array. Orio. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 15, 2007 Share Posted August 15, 2007 <?php print_r(glob("*.{gif|jpg|jpeg|png}", GLOB_BRACE)); ?> ... :-\ Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 15, 2007 Author Share Posted August 15, 2007 excellent. thanks so very much Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 so i want to clear up my output a big. i want the names to appear without the extension and with capitals at the begining and replace and hyphens and underscores with spaces. im messing with strings heres what i have to start with: //determines the name $filename = "thIs_is-iT for sURe.jpg"; //gets the position of the last occurence of the char $filenamea = strrpos($filename, "."); //displays the filename without the ext $filenameb = substr($filename, 0, $filenamea); //set replace array $to_replace = array("-","_"," "," "); //replace the chars $filenamec = str_replace($to_replace, " ", $filenameb); //make all lower case $filenamed = strtolower($filenamec); i do realise this is rather messy and if there is an easier way please point me towards it! but this is only practise. the only problem is now i cant find a function to turn the first letter into a capital so the name ends up like this: "This is it for sure" is there such a function? Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 16, 2007 Share Posted August 16, 2007 is there such a function? Yes - ucfirst() - http://ca.php.net/manual/en/function.ucfirst.php Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 woop, thank you now the only problem is that when i make this into a function and echo the names of the files it seems to cause an error and it doesnt completely list all the files and such. well to be honest im prbably just fucking it up by not doing it right... im not completely up on functions.. :/ which i guess is a bad thing here my completed source code: <?php function changeName($file,$filenamee){ //determines the name $filename = $file; //gets the position of the last occurence of the char $filenamea = strrpos($filename, "."); //displays the filename without the ext $filenameb = substr($filename, 0, $filenamea); //set replace array $to_replace = array("-","_"," "," "); //replace the chars $filenamec = str_replace($to_replace, " ", $filenameb); //make all lower case $filenamed = strtolower($filenamec); //make first character uppercase $filenamee = ucfirst($filenamed); //echo($filenamea); //echo($filenameb); //echo($filenamec); //echo($filenamed); echo($filenamee); } $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><ul>"; foreach ($dirs as $file) echo "<li><a href='".$file."'>" changeName($file,$filenamee); echo "</a></li>"; echo "</ul>"; } if(count($imgs) > 0) { echo "<b>Images</b><ul>"; foreach ($imgs as $file) echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> ".$file."</a></li>"; echo "</ul>"; } if(count($others) > 0) { echo "<b>Other Files</b><ul>"; foreach ($others as $file) echo "<li><a href='".$file."'>".$file."</a></li>"; echo "</ul>"; } ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 <?php function changeName($file) { $path_parts = pathinfo($file); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $path_parts['filename']))); } ?> Shorter and uses a function which is supposed to get the different parts of a file: pathinfo() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.