Find the answer to your Linux question:
Results 1 to 5 of 5
Hello - I was curious, using PHP, is it possible to 1) read the contents of a folder on the server, how? 2) Display and link to that contents of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Jun 2003
    Posts
    160

    Reading contents of Directory


    Hello -
    I was curious, using PHP, is it possible to

    1) read the contents of a folder on the server, how?

    2) Display and link to that contents of that folder, how?

    3) provide a simple method for file management, how?

    4) integrate with a payment solution, how?

    Cole

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Hrmph... it's directory, not folder. ;-)

    Anyhow, to read directory entries, just use the opendir and readdir functions, like this:
    Code:
    if($dir = opendir("/tmp"))
    {
        while(($dentry = readdir($dir)) !== FALSE)
            print("$dentry
    \n");
    } else {
        print("Burp...\n");
    }
    As for linking to that directory, I'm not completely sure what you mean.

  3. #3
    Linux Newbie
    Join Date
    Jun 2003
    Posts
    160

    folders...directories...

    Sorry - I use linux at home and a Mac at work, everyone here calls them folders

    As for what I mean - If an item in a directory is another directory, provide a link which will take me to view the contents of that directory. If it is a file, use the script that I've pre-written to open that particular file (i.e. jpg and GIF) based on file extension.

    What about creating and removing directories?

    Cole

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, how about something like this:
    Code:
    $rootdir = "/var/www";
    if(!($sdir = $HTTP_GET_VARS["dir"]))
        $sdir = "";
    $dir = $rootdir . $sdir;
    if(!($dir = opendir($dir)))
    {
        print("Burp...\n");
        exit(0);
    }
    while(($dentry = readdir($dir)) !== FALSE)
    {
        if(is_dir($dir . "/" . $dentry))
            print&#40;"<a href=\"this.php?dir=" . $sdir . "/" . $dentry ."\">" . $dentry . "</a>
    \n";
        else
            print&#40;$dentry . "
    "&#41;;
    &#125;
    I don't write much PHP these days, and I haven't tried that, so it's very possible that it doesn't work, but I guess that it should give you an idea.
    As for creating directories, I would create a form which gives the new directory's name as a POST variable, and then the script can create it if that POST variable is present. As for removing them, I'd create an extra link on each directory, that gives some GET variable that indicates that the directory in the argument should be deleted if its value is present.
    Of course, that's just a very simple solution, but I don't know how serious this is supposed to be.

  5. #5
    Linux Newbie
    Join Date
    Jun 2003
    Posts
    160

    it helps

    Thanks - it's for a silly project that I'm working on - Thanks for the help - it's enough to at least help me get started

    Cole

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •