PHP Code

Posted on 2011-10-21
Last Modified: 2012-06-27
Can any one help me. I need a simple PHP code that will read the content of an online folder on my wbt site for example I will like to know the name of the file ext (I wish only to read MP3) I would like to keep a list of the files that are currently in the folder
Question by:Zirwo01
    LVL 7

    Accepted Solution

    This is based on code found from

      //$current_dir = "$DOCUMENT_ROOT"."/";    //Put in second part, the directory - without a leading slash but with a trailin$
      $current_dir = "./";
      $dir = opendir($current_dir);        // Open the sucker
      echo ("<p><h1>List of available files:</h1></p><hr><br />");
      while ($file = readdir($dir))            // while loop
        $parts = explode(".", $file);                    // pull apart the name and dissect by period
        if (is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part
            $extension = end($parts);        // set to we can see last file extension
            if ($extension == "mp3" OR $extension == "MP3")    // is extension ext or EXT ?
                 echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />";    // If so, echo it out else do nothing cos it's$
      echo "<hr><br />";  
      closedir($dir);        // Close the directory after we are done

    Open in new window


    Author Comment

    This is awsome what about if I would like it in aphabeticle order?
    LVL 3

    Expert Comment

    I guess what you need is some .htaccess
    for example the url you provided could be

    and if you can make your system like this e.g.: file.php?name=xxxxxxxx

    then with some htaccess tricks you can change the way url is shownn

    Author Comment

    I am not following you this for the read folder content?
    LVL 7

    Expert Comment

    It appears as if they misread the question, in that thinking you have a php script, and you want it to appear as if it is a directory. Which, there are simpler ways than using .htaccess to do that as well ('d have just made a folder named /music/ and named the script index.php and placed it in the folder).

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now