Hi all, I've been gone a while.

Um, does anyone know how to return the number of files in a folder?
Example:
.../folder/file1.gif
.../folder/other.htm
/folder/ contains 2 files.

Only without listing the files. All I want is a number.

Any help? URGENT!

    echo count(scandir('/directory')-2);
    

      Wait. Why minus two???
      Also, does this work if the PHP script is in the same folder and without using "/" at the beginning, e.g.

      $range = count(scandir('folder-script-is-in')-2);

      only with just nothing in between the quotes or something?

        If you want to examine the current directory, try using a singe period.

        Also, he's subtracting two because of two entries that are returned: . and .. which mean the current directory and the parent directory, respectively.

          <?php
          
          $range=count( scandir( '.' ) )-2;
          
          echo "there are <b>$range</b> files+folders in this folder<br>";
          echo "<br>this does not include<br><b>.<br>..</b>";
          
          ?>

          -2 because of those two links you can see in at the top of a directory listing
          .
          ..

          file1.php
          file2.php

          Note:
          also folders counts as files with scandir()
          I also had to place '-2' OUTSIDE of parantes, otherwise error

            Is there a ... for parent parent directory?
            e.g. folder/subfolder/subsubfolder/script.php
            would mean I'd have to do -3?
            If so, is there a way to determine how many parent folders there are so this minus thing works automatically no matter where the script is installed and gets the correct amount of files?

              Is there a ... for parent parent directory?
              e.g. folder/subfolder/subsubfolder/script.php
              would mean I'd have to do -3?

              What?

              Maybe this will explain it.

              $dircontents = scandir('.');
              print_r($dircontents);
              

                I'll try that. I'm just asking, since you had minus two for the folder and its parent (. and ..), does that mean that if the parent has its own parent (e.g. grandparent/parent/folder/script.php is where the script is located) would I have to do minus three for ., .., and ...?

                  lupus2k5 wrote:

                  Is there a ... for parent parent directory?
                  e.g. folder/subfolder/subsubfolder/script.php
                  would mean I'd have to do -3?
                  If so, is there a way to determine how many parent folders there are so this minus thing works automatically no matter where the script is installed and gets the correct amount of files?

                  no
                  you can not jump over one folder level, you have to walk one step by one step in folder level
                  . means current directory
                  .. means parent
                  /subdirecory/ ______________ of course is child

                  See my post above.
                  I added some info and change.

                    OK, thanks, I get it. But my script doesn't work and I don't know why.
                    Okay, here's my script so you can see it. It's a quote of the week script. I know it's not exactly the ideal way to make a QOTW script, but my friend wrote it and just asked me to help him fix it and he's too lazy to post on a forum about it. So, uh, yeah.
                    qotw.php

                    <?php
                    // ########################
                    // Quote of the Week script
                    // ########################
                    include("config.php"); // Include configuration file
                    $currentdate = date("m.d.Y"); // Find today's date
                    $day = date("l"); // Find the day of the week
                    $read = fopen("date.txt","r"); // Open date.txt to read
                    $contents = fread($read,filesize("date.txt")); // Read date.txt
                    if($contents = $currentdate) { // If date.txt's date = today...
                    $rewrite = "0"; } // don't make a new quote
                    else { $rewrite = "1"; } // Else do make a new quote
                    fclose($read); // Close date.txt
                    if($day = "Monday" && $rewrite == "1") { // If today is Sunday and the quote hasn't yet been changed... SET TO MONDAY FOR TESTING PURPOSES
                    $range = count(scandir('quotes')-1); // Find the number of quotes
                    $rand = rand(0,$range); // Choose random number
                    $randqotw = "quotes/qotw".$rand.".txt"; // Generate txt name
                    $file = fopen("quote.txt","w"); // Open quote.txt to write to
                    fwrite($file,$randqotw); // Write the random quote chosen to quote.txt
                    fclose($file); // Close quote.txt
                    $file2 = fopen("date.txt","w"); // Open date.txt
                    fwrite($file2,$currentdate); // Write today's date to date.txt
                    fclose($file2); // Close date.txt
                    include("quote.txt"); // Display quote
                    exit; } // Exit
                    else { // If today isn't Sunday and/or it's already been written...
                    include("quote.txt"); // Display quote
                    }
                    ?>

                    date.txt

                    DATE

                    GETS EDITED WHEN SCRIPT UPDATES SUNDAY

                    quote.txt

                    QUOTE

                    GETS EDITED WHEN SCRIPT UPDATES SUNDAY

                    qotw/1.txt

                    quote1

                    etc etc

                      Change this:

                      $range = count(scandir('quotes')-1);

                      to this:

                      $range = count(scandir('quotes/')) - 1;

                      EDIT: To answer your question above, to go back two directorires (parent of the parent), you would just use ../../ as the path.

                        here is another version

                        will produce a list of all folders and files
                        like LINKS
                        each file/folder will open in a new window when clicked ( target="_blank" )

                        <?php
                        
                        $files = scandir( '.' );
                        
                        echo "<hr>";
                        
                        foreach ( $files AS $name )
                            echo '<a href="' . $name . '" target="blank">' . $name . '</a><br>';
                        
                        echo "<hr>";
                        
                        ?>

                        At the scandir() php.net functions manual
                        you can find many user contributed scripts, in comments if you scroll down
                        Some which will scan the whole directory tree at your webserver
                        for files and subfolders and subfolders/subfolders ...
                        and make listings displayed.
                        Go see: http://php.net/scandir

                        See Also examples in these functions:
                        http://php.net/readdir
                        http://php.net/opendir
                        http://php.net/is_dir
                        http://php.net/glob
                        http://php.net/sort

                          Actually, I think I have an error somewhere else. I set it to Tuesday since today is Tuesday (I don't know if it is in GMT, I should check that). But it just loads "?>QUOTE" when I open qotw.php...

                            You should check your file "quote.txt" really only have contents
                            QUOTE

                            I really cant say wherefrom came '?>'
                            in output
                            ?>QUOTE

                            I think this wont work:
                            $currentdate = date("m.d.Y");

                            see function [man]date[/man]
                            it takes a second parameter, usually time()
                            = current time in seconds
                            okay, you are right:
                            if there is no second parameter, then is assumed current time
                            which is = time()

                            so
                            $currentdate = date("m.d.Y");
                            will work!

                            1) I Turned ON full debug error_reporting on line 2

                            You have to make these following changes, 2) 3)
                            2) if($contents = $currentdate) should be if($contents == $currentdate)
                            3) if($day = "Monday" && should be if($day == "Monday" &&

                            4) I added an exit; for // If today isn't Sunday

                            <?php
                            error_reporting(2047); // = (E_ALL); debug setting
                            // ########################
                            // Quote of the Week script
                            // ########################
                            include("config.php"); // Include configuration file
                            $currentdate = date("m.d.Y"); // Find today's date
                            $day = date("l"); // Find the day of the week
                            $read = fopen("date.txt","r"); // Open date.txt to read
                            $contents = fread($read,filesize("date.txt")); // Read date.txt
                            if($contents = $currentdate) { // If date.txt's date = today...
                            $rewrite = "0"; } // don't make a new quote
                            else { 
                            $rewrite = "1"; } // Else do make a new quote
                            fclose($read); // Close date.txt
                            
                            if($day = "Monday" && $rewrite == "1") { // If today is Sunday and the quote hasn't yet been changed... SET TO MONDAY FOR TESTING PURPOSES
                            $range = count(scandir('quotes')-1); // Find the number of quotes
                            $rand = rand(0,$range); // Choose random number
                            $randqotw = "quotes/qotw".$rand.".txt"; // Generate txt name
                            $file = fopen("quote.txt","w"); // Open quote.txt to write to
                            fwrite($file,$randqotw); // Write the random quote chosen to quote.txt
                            fclose($file); // Close quote.txt
                            $file2 = fopen("date.txt","w"); // Open date.txt
                            fwrite($file2,$currentdate); // Write today's date to date.txt
                            fclose($file2); // Close date.txt
                            include("quote.txt"); // Display quote
                            exit; // Exit
                            }
                            else { // If today isn't Sunday and/or it's already been written...
                            include("quote.txt"); // Display quote
                            exit; // added Exit
                            }
                            
                            // exit; // two previous exits could be replaced by one here
                            
                            ?>
                              Write a Reply...