Timma Posted August 29, 2007 Share Posted August 29, 2007 Hi there, I'm wondering how I should go about creating a sort of dynamic list. I have a folder, let's just call it folder, which is filled with files called... file01.jpg file02.jpg and so on, there is also a folder in folder called folder1. These are all contained in localhost/site/. What I am wanting to do on a php page is list all the "files" inside folder, but not show folder1 in that list, and also for the files to be listed correctly, not like 1, 10, 11, ..., 2, 21, etc. Plus above some files there will be headings. (These are all links as well to a php page files.php?#) And finally I would like to know how to, instead of displaying the filename, which would usually happen, but instead just cutting off the word "file" and the ".jpg" after the number. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/ Share on other sites More sharing options...
Timma Posted August 29, 2007 Author Share Posted August 29, 2007 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-337064 Share on other sites More sharing options...
schme16 Posted August 29, 2007 Share Posted August 29, 2007 Here ya go Timma, hope this helps! (I tried to comment it enough lol) function list_files($dir) { //checks the directory given to make sure its actually a directory. if(is_dir($dir)) { //opens the directory for reading. if($handle = opendir($dir)) { //reads each file in the directory. while(($file = readdir($handle)) != false) { //eliminates the files listed frombeing read. if($file != "." && $file != ".." && $file != "Thumbs.db" /*pesky windows, images..*/) { //this outputs the files name print"$file <br />"; } } closedir($handle); } } } Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-337142 Share on other sites More sharing options...
Timma Posted August 29, 2007 Author Share Posted August 29, 2007 Thanks, but that only answers one question. Actually that doesn't work D: But still, could people look at the other things too. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-337162 Share on other sites More sharing options...
piznac Posted August 29, 2007 Share Posted August 29, 2007 Actually that doesn't work D: Actually it works quite well. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-337229 Share on other sites More sharing options...
Timma Posted August 29, 2007 Author Share Posted August 29, 2007 Fine then, when I attempt using it doesn't work. Rephrased, happy? Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-337356 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 What I am wanting to do on a php page is list all the "files" inside folder, but not show folder1 in that list, and also for the files to be listed correctly, not like 1, 10, 11, ..., 2, 21, etc. Plus above some files there will be headings. (These are all links as well to a php page files.php?#) And finally I would like to know how to, instead of displaying the filename, which would usually happen, but instead just cutting off the word "file" and the ".jpg" after the number. Still need help with these two, bump. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338461 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 Bump, 'cause I can. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338554 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 you may want to post the way your using the list_files function as for the 2nd question replace print"$file <br />"; with $filedisplay = preg_replace('/[a-z.]/si', '', $file); print"$filedisplay <br />"; Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338562 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 Hmm, thanks I guess. Could you please explain the second method a bit more? (My mind is completely blank on the first answer ) Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338567 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 basically it replaces the letters and the dot with nothing thus leaving the number only the first code should start like this list_files("data/myimages/") function list_files($dir) { //...snip Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338571 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 Okay, well as I'm using a server on my PC I just link to the C:/ folder which it is in, this works with my other script, which I found somewhere on the interwebs. I used list_files with ("C:/WEB_SERVER/www/site/folder/") And I'm using this script now: <?php $path = "C:/WEB_SERVER/www/site/folder/"; // Open the folder $dir_handle = opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" ) continue; echo "<a href=\"$file\">$file</a><br />"; } // Close closedir($dir_handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338574 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 with that code you could just change echo "<a href=\"$file\">$file</a><br />"; to $filedisplay = preg_replace('/[a-z.]/si', '', $file); echo "<a href=\"$file\">$filedisplay</a><br />"; for the numbering.. as for the path ($path = "C:/WEB_SERVER/www/site/folder/" shouldn't that be $path = "site/folder/"; ? Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338581 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 No idea, that directory works, so I use it. This script said I had to use it directly from the root of the website. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338585 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 C:/WEB_SERVER/www/ is the windows path to the root of the site, thus if your script calls a file from the root (/) then it will look from their.. hope that makes sense! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338587 Share on other sites More sharing options...
schme16 Posted August 31, 2007 Share Posted August 31, 2007 to get the root dynamically you can use the function : getcwd()...... Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338589 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 /** * Change the path to your folder. * This must be the full path from the root of your * web space. If you're not sure what it is, ask your host. * * Name this file index.php and place in the directory. */ // Define the full path to your folder from root $path = "/home/content/s/h/a/shaileshr21/html/download"; Well, I assumed home was like C:/ and html was like www so therefore that's what I did, I used the C:/......./www/.... thing. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338596 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 Windows(system) Root = c:\ but if the webserver also used then when someone came to your site they could see all your files etc.. so the webserver asks where your webefile/site is stored (or it has a default), now that path is the web servers root.. so you on your computer see c:\ as the root but other people connecting throught your server see the webservers root.. so when it refers to root its infact refering to the webroot ("root of your web space"). Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338607 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 So when uploaded to an actual web server, rather than my computer what modifications to the path would I have to do? Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338612 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 use Relative Paths Relative Path URLs Relative paths change depending upon what page the links are located on. There are several rules to creating a link using the relative path: * links in the same directory as the page have no path information listed filename * sub-directories are listed without any preceding slashes weekly/filename * links up one directory are listed as ../filename How to determine the relative path: 1. Determine the location of the page you are editing. This article is located in the/library/weekly folder on my site. 2. Determine the location of the page or image you want to link to. The Beginner's Resource Center is located here: /library/beginning/ 3. Compare the locations and to decide how to point to it From this article, I would need to step up one directory (to/library) and then go back down to the beginning directory 4. Write the link using the rules listed above: <a href="../beginning/bl_begin.htm"> ...</a> as for the Dirlist, try this // get the Absolute Path (see link above) $path = dirname(__FILE__)."/"; //goes up a folder to a folder called images $path .= "images/"; Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338620 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 Alright everything works, yet there are headings which need to be there, for example my site should look like... Heading 1 - 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11 Heading 2 - 12, 13, 14 ,15 ,16 Heading 3 - 17, 18, 19, 20, 21, 22, 23, 24 Yet obviously it looks like: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338622 Share on other sites More sharing options...
Timma Posted August 31, 2007 Author Share Posted August 31, 2007 Bump, 'cause I'm allowed. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-338991 Share on other sites More sharing options...
MadTechie Posted September 1, 2007 Share Posted September 1, 2007 can you please explain the logic behind the headers.. why would 1 go from 1 to 11 but header 2 only go from 12 to 16 ? Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-339016 Share on other sites More sharing options...
Timma Posted September 1, 2007 Author Share Posted September 1, 2007 Well with that I was basically showing that it isn't a constant number each time. Also the headings aren't actually Heading 1 and Heading 2 they are completely different words every time. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-339054 Share on other sites More sharing options...
AndyB Posted September 1, 2007 Share Posted September 1, 2007 Well with that I was basically showing that it isn't a constant number each time. Also the headings aren't actually Heading 1 and Heading 2 they are completely different words every time. We'll need to use php's read_mind() function to help more. Quote Link to comment https://forums.phpfreaks.com/topic/67195-solved-directory-listing/#findComment-339075 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.