www.webdeveloper.com
Results 1 to 6 of 6

Thread: get file in folder

  1. #1
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197

    get file in folder

    Hi

    I'm trying to figure out script on how to list all files in a directory then when the directory name is selected then get all files in the subdirectoy.

    lastly script that will get all files in the in the directory then the sub directory.

    thanks
    Kevin

  2. #2
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    I found the below but it just returns array

    PHP Code:
    function dir_list($dir)
    {
      
    $dl = array();
      if (
    $hd opendir($dir))
      {
        while (
    $sz readdir($hd)) { if (preg_match("/^\./",$sz)==0$dl[] = $sz; }
        
    closedir($hd);
      }
      
    asort($dl);
      return 
    $dl;
    }

    $file_folder 'user_images/';

    echo 
    dir_list($file_folder); 
    Kevin

  3. #3
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    this code works but it lists the files and the directory names, I just want the directory names

    any ideas how to change the code

    site where I found the code
    http://snippets.dzone.com/posts/show/155

    PHP Code:
    function directoryToArray($directory$recursive) {
        
    $array_items = array();
        if (
    $handle opendir($directory)) {
            while (
    false !== ($file readdir($handle))) {
                if (
    $file != "." && $file != "..") {
                    if (
    is_dir($directory"/" $file)) {
                        if(
    $recursive) {
                            
    $array_items array_merge($array_itemsdirectoryToArray($directory"/" $file$recursive));
                        }
                        
    $file $directory "/" $file;
                        
    $array_items[] = preg_replace("/\/\//si""/"$file);
                    } else {
                        
    $file $directory "/" $file;
                        
    $array_items[] = preg_replace("/\/\//si""/"$file);
                    }
                }
            }
            
    closedir($handle);
        }
        return 
    $array_items;
    }

    $files directoryToArray("user_images/proctk/"true);

    echo 
    '<ul>';

    foreach (
    $files as $file) {
        echo 
    '<li>' $file '</li>';
    }

    echo 
    '</ul>';
    ?> 
    Kevin

  4. #4
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    figured it out change the true to false.

    this raises another question. how would I get just the file names and not the directories
    Kevin

  5. #5
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    Alright

    I keep posting to this, I solve an issue then another issue pops up

    here is the line of code that i causing a problem

    $files = directoryToArray("../user_images/".$user_name."/", false);


    The ../user_images/
    is the problem. This code is on a page that is in a sub folde so I need to get back to the root directory. If I us the same string of code on a page in the root directory then it works fine.

    any ideas how to fix this
    Kevin

  6. #6
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    I'm getting this error message, thought I would share this info as I may be getting closer

    PHP Code:
    opendir(http://familyclick.ca/user_images/proctk/) [function.opendir]: failed to open dir: not implemented in 
    Kevin

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles