Jump to content

[SOLVED] Directory listing


Timma

Recommended Posts

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.

Link to comment
Share on other sites

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);
    }
  }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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); 
?>

Link to comment
Share on other sites

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/";

?

 

Link to comment
Share on other sites

/**
* 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.

Link to comment
Share on other sites

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").

 

:)

Link to comment
Share on other sites

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/";

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.