Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts

    how to list all the files in current directory

    i uploaded pictures to a directory and in that directory i will make an index.php file and with this file i want to be able to list all the pictures in this folder clickable so that my friend who lives far from me can click each of them and look at it. how can i do that?

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    PHP Code:
    $folderPath 'whereismyfiles';
    $fList = array();
    if (
    $handle opendir($folderPath)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && substr($file,0,2) != "._" && $file != "..") {
                
    $fList[] = $file;
            }
        }
        
    closedir($handle);
    }
    // this is the files list
    sort($fList);
    print_r($fList); 

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmj001 View Post
    PHP Code:
    $folderPath 'whereismyfiles';
    $fList = array();
    if (
    $handle opendir($folderPath)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && substr($file,0,2) != "._" && $file != "..") {
                
    $fList[] = $file;
            }
        }
        
    closedir($handle);
    }
    // this is the files list
    sort($fList);
    print_r($fList); 
    thanks, for the 'whereismyfiles' i typed '../mycurrentfoldername' and it listed the file names but how can i make each item in the list a clickable item so that when clicked the picture will open?

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    PHP Code:
    // loop through the files and read the functions
    $j count($fList);
    for(
    $i 0$i $j$i++){
         echo 
    '<a href="">'.$fList[$i].'</a><br>';

    you will need to figure out the href=""

  • Users who have thanked jmj001 for this post:

    ketanco (01-29-2012)

  • #5
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Here's a solution which will just iterate the images for you and make them clickable.

    PHP Code:
    <? function getFileExtension($filename)
    {
      
    $path_info pathinfo($filename);
      return @
    $path_info['extension'];
    }

     function 
    getFileList($dir)
      {
        
    // array to hold return value
        
    $retval = array();

        
    // add trailing slash if missing
        
    if(substr($dir, -1) != "/"$dir .= "/";

        
    // open pointer to directory and read list of files
        
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while(
    false !== ($entry $d->read())) {
          
    // skip hidden files
          
    if($entry[0] == ".") continue;
          if(
    is_dir("$dir$entry")) {
            
    $retval[] = array(
              
    "name" => "$dir$entry/",
              
    "type" => filetype("$dir$entry"),
              
    "size" => 0,
              
    "lastmod" => filemtime("$dir$entry")
            );
          } elseif(
    is_readable("$dir$entry")) {
            
    $retval[] = array(
              
    "name" => "$dir$entry",
              
    "type" => getFileExtension("$dir$entry"),
              
    "size" => filesize("$dir$entry"),
              
    "lastmod" => filemtime("$dir$entry")
            );
          }
        }
        
    $d->close();

        return 
    $retval;
      }
      
    $dirlist getFileList(".");
    ?>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pictures</title>
    </head>
    <blockquote><br />
     // <b>Pictures</b> //<br /><br /><table border="1">
    <tr><th width="200">Name</th><th>Size</th><th>Last Mod.</th></tr>
    <?PHP
      
    // output file list as table rows
      
    foreach($dirlist as $file) {
        if((
    $file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;
        echo 
    "<tr>\n";
        echo 
    "<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
        echo 
    "<td>{$file['size']}</td>\n";
        echo 
    "<td>",date('r'$file['lastmod']),"</td>\n";
        echo 
    "</tr>\n";
      }
    ?>
    </table>
    <body>
    </body>
    </html>

  • #6
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmj001 View Post
    PHP Code:
    // loop through the files and read the functions
    $j count($fList);
    for(
    $i 0$i $j$i++){
         echo 
    '<a href="">'.$fList[$i].'</a><br>';

    you will need to figure out the href=""
    ok now it lists the files as clickable links but when i click does not open the pictures. how can i open that picture when i click its link?
    Last edited by ketanco; 01-29-2012 at 09:38 PM.

  • #7
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss.

  • #8
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weir-07 View Post
    The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss.
    actually i tried yours too but it only listed the titles of a table and that was it. i copied and pasted your code as it is. what might be wrong?

  • #9
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Woops should have tested that!

    Remove the line;
    PHP Code:
     if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 

  • Users who have thanked weir-07 for this post:

    ketanco (01-29-2012)

  • #10
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weir-07 View Post
    Woops should have tested that!

    Remove the line;
    PHP Code:
     if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 
    woow very nice thanks a lot!!! it even zooms in once i open picture....

  • #11
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weir-07 View Post
    Woops should have tested that!

    Remove the line;
    PHP Code:
     if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 

    one more thing... what if i wanted the pictures to display without clicking, as soon as i open the webpage? what part of the code would i change and how? (i know for many pictures it would be a very very slow opening webpage but thats another thing...)

  • #12
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Make a html document.
    Name it index.
    Then insert your pictures into that.

    You can google how to do that, I'm sure

  • #13
    Regular Coder
    Join Date
    Jul 2009
    Posts
    193
    Thanks
    26
    Thanked 0 Times in 0 Posts
    ok i know how to do that but for that i would have to type all file names one by one... with your method, i dont... what if i want to share 400 photos with my friend... thats whyi asked that

  • #14
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Coming back to this, just change the a tags to img tags, with the src as $file['name'] OR change the inside of the a tag an img tag, again with $file['name'] as the src.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #15
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I can't believe no one has suggested using glob() yet:
    PHP Code:
    $files glob'./files/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}'GLOB_BRACE );

    foreach ( 
    $files as $file )
    {
        echo 
    '<a href="' $file '">'basename$file ), '</a><br />';

    Last edited by kbluhm; 01-30-2012 at 10:33 PM.


  •  

    Posting Permissions

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