Get folder name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • memermore
    New Member
    • Sep 2007
    • 15

    Get folder name

    How can I get all folder names from a specific directory and list them using PHP?
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Originally posted by memermore
    How can I get all folder names from a specific directory and list them using PHP?
    Check out the scandir function. This should help you out.

    Comment

    • memermore
      New Member
      • Sep 2007
      • 15

      #3
      Originally posted by stepterr
      Check out the scandir function. This should help you out.
      It didn't work. i received a "call to undefined function" error. is this has something to do with my PHP version? I'm using PHP 4

      Comment

      • svenni
        New Member
        • Jan 2008
        • 4

        #4
        scandir is PHP5, so to do the same in PHP4 you would need a different approach, which means a couple more lines of code.

        I've done this some time ago, but I couldn't find my own code at the moment, so I found a similar code here :
        [PHP]
        <?php
        // declare the folder
        $ourDir = "/home/public_html/folderName";

        // prepare to read directory contents
        $ourDirList = @opendir($ourDi r);

        // loop through the items
        while ($ourItem = readdir($ourDir List))
        {
        // check if it is a directory
        if (is_dir($ourIte m))
        {
        echo "directory: $ourItem <br />";
        }
        // check to see if it is a file
        if (is_file($ourIt em))
        {
        echo "file: $ourItem <br />";
        }
        }

        closedir($ourDi rList);
        ?>
        [/PHP]
        This should work in PHP4 as well as PHP5.

        Comment

        • stepterr
          New Member
          • Nov 2007
          • 157

          #5
          Yep, svenni is correct that is a PHP 5 function. Svenni's method should work for what you want. If you want to see more directory functions, check out this site

          Comment

          • memermore
            New Member
            • Sep 2007
            • 15

            #6
            That is exactly what I'm trying to get. thanks guys! By the way do you any ways of searching a file from a specific directory? including its subdirectories? and display a list of search results. As of now I'm thinking of using a database for that but I'm still hoping for a better alternative

            Comment

            • svenni
              New Member
              • Jan 2008
              • 4

              #7
              Originally posted by memermore
              That is exactly what I'm trying to get. thanks guys! By the way do you any ways of searching a file from a specific directory? including its subdirectories? and display a list of search results. As of now I'm thinking of using a database for that but I'm still hoping for a better alternative
              I think listing all the files in a database might be a good idea, but it depends on the amount of files. It shouldn't be a big problem for PHP to loop through lets say 100 files and return the ones which match your criteria, but if you have more than 1000 files I guess it would be best to use the indexing features in MySQL to search faster. The problem would though be the fact that the database needs to be updated once in a while, and I'm not quite sure about how often.

              Anyways, good luck!

              Comment

              • Lightlord
                New Member
                • Jun 2008
                • 1

                #8
                I could use some help with almost something similar:


                I am trying to get php to read the folder name that php is in and write it to a text file.

                i.e. www.mydomain.com/john

                I need the script to write john to a text file

                although this will not work; something close:

                <?php

                $_SERVER["REQUEST_UR I"]
                $your_data =($_SERVER);

                // Open the file and erase the contents if any

                $fp = fopen("textfile _name.txt", "w");

                // Write the data to the file
                fwrite($fp, $your_data);

                // Close the file
                fclose($fp);

                ?>




                The overall all goal is to also perform the following if possible using php.



                when it write the folder name:

                the folder named one of the two ways:

                /bob
                /mary-jane

                can it upppercase to Bob
                can it upper case to Mary Jane and drop the -


                anyone have any ideas?


                The end result is flash to read the txt file and so it can write it into the flash sction script.


                The part I am not sure is after php writes it to the text file, will it happen before flash reads the txt file?



                Thanks for any assistance


                LL

                Comment

                • tazosmr
                  New Member
                  • Feb 2012
                  • 4

                  #9
                  i've done that job with the code, which gets and outputs the file path:

                  <?php
                  $Path=$_SERVER['REQUEST_URI'];
                  $URI='http://www.example.com '.$Path;

                  echo "$URI";
                  ?>

                  Comment

                  Working...