1. This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
    For information on cookies and how you can disable them visit our Cookie Usage page.
    Dismiss Notice

PHP File Counter

Discussion in 'Web Development' started by pezboy45, Oct 18, 2008.

  1. pezboy45

    pezboy45 Mod/Design & Coder [Pro]

    Ok, so here is what I need/looking for.
    I need a php(java works too) script that counts the amount of a certain file in a folder.
    I've seen it done, but I can't seem to find a way (that works)

    Basically I just need it to say: Currently Shuffling __ Songs!
    So, if anyone could help me on this..that would be great!

    Thanks!
     
  2. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    This part has got me confused
    Do you want to only count a certain file type in a directory or all files in that directory and then display Currently Shuffling 12 Songs! or whatever the count is?
     
  3. pezboy45

    pezboy45 Mod/Design & Coder [Pro]

    Yea, like I want it to only count .mp3 files.
     
  4. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    I'll see what I can do.
     
  5. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    Give this a try.

    <?php
    $total_mp3_file=count(glob("*.mp3"));
    echo "Currently Shuffling ".$total_mp3_file "Songs";
    ?>
    The above example will take from the current directory. To search a specific directory you can use the following. (changes the first line)
    To list number of mp3 on a particular directory you can use something like this.
    <?php
    $total_mp3 = count(glob("/path/to/mp3/{*.mp3}", GLOB_BRACE));
    ?>

    ************************************************************************
    to simply get a count of files in a given directory

    <?php
    //get path of directory containing this script
    $dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF);
    //open a handle to the directory
    $handle = opendir($dir);
    //intitialize counter
    $count = 0;
    //loop through the directory
    while (false !== ($file = readdir($handle))) {
    //evaluate each entry, removing the . & .. entries
    if (is_file($file) && $file !== '.' && $file !== '..') {
    $count++;
    }
    }
    echo $count;
    ?>
    or if you prefer, assign the $file variable to an array in the loop:
    if...{
    $files[$count] = $file;
    $count++;
    }
    then you can return either $count or count($files) to get your number of files.
     
  6. pezboy45

    pezboy45 Mod/Design & Coder [Pro]

    Wow, I'm testing it right now!!!
    Thanks!!
    \\\\

    Ok, I get an error:
    Code:
    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/.uris/dahost/infinisound/index.php on line 107
    I'm trying to figure out the second code you game me, the one after all the "*****".
    But, if you have any answers let me know.
    Thanks.
     
  7. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    let me see your code.
     
  8. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    Forgot - This error is encountered when running on PHP 4.x and on a shared hosting. Update to PHP ver 5.x and try again.
     
  9. pezboy45

    pezboy45 Mod/Design & Coder [Pro]

    I'm running 5.x.x
    I am on shared hosting, but that shouldn't really effect it because we all have our own virtual servers.
    So, I'm really confused on what to do.

    (Thanks for your help though)
     
  10. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    Is that your girlfriend??? or is she just WOWing us.
    Parse error: could be the line it is showing or it could be a line somewhere else. Hard for me to guess at, easier with the code, to look at to solve problem. Promise I won't give it away.
    yes you are right shared server is not a problem.
     
  11. pezboy45

    pezboy45 Mod/Design & Coder [Pro]

    Ok, so...
    I've tried:
    PHP:
               <?php
                    $total_mp3 
    count(glob("/home/.uris/dahost/infinisound/streamr/{*.mp3}"GLOB_BRACE));
                    echo 
    "Currently Shuffling ".$total_mp3_file ."Songs";
                
    ?>
    And that doesn't work, but I don't get any errors.
    I've tried:
    PHP:
                <?php
                    $total_mp3 
    count(glob("http://infinisound.dahoster.org/streamr/{*.mp3}"GLOB_BRACE));
                    echo 
    "Currently Shuffling ".$total_mp3_file ."Songs";
                
    ?>
    again, no error but it also doesn't show the number of songs.

    So, I've fixed the error problem by putting anoter "." at the end of .$total_mp3_file
    But, I can't figure out how to get it to show the actual number.