SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how to display all files from a folder using php

    Hi i want to display files only from a folder,how to check if it is file or folder .give any sample code ,it will be very useful to me.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Everything you need can be found on PHP.net.

    is_file()
    is_dir()
    opendir()

  3. #3
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    // open the current directory
    $dhandle =  opendir"./");
    // define an array to hold the files
    $files = array();

    if (
    $dhandle) {
       
    // loop through all of the files
       
    while (false !== ($fname readdir($dhandle))) {
          
    // if the file is not this file, and does not start with a '.' or '..',
          // then store it for later display
          
    if (($fname != '.') && ($fname != '..') && 
              (
    $fname != basename($_SERVER['PHP_SELF']))) {
              
    // store the filename
              
    $files[] = (is_dir"./$fname)) ? "(Dir) {$fname}$fname;
          }
       }
       
    // close the directory
       
    closedir($dhandle);

    this code retrive foldernames too. what wrong in it .i want to display the files name only

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The following line is why everything is been included in the $files array.

    PHP Code:
    $files[] = (is_dir"./$fname)) ? "(Dir) {$fname}$fname
    To retrieve only files the following will work
    PHP Code:
    if (is_file($fname)) {
        
    $files[] = $fname;


  5. #5
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i tried to delete the file using unlink command but it is not work. is it we have to pass extension in it. its extension is file .

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No, the while loop contains the filename + the extension so no further logic is needed, my question to you is have you checked your error log as if unlink() is failing it's most likely due to a permissions error.

  7. #7
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you. its path problem i didnt give path in unlink . now it solves


Bookmarks

Posting Permissions

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