PHP iterating directories with ftp_ functions

149 views Asked by At

Ok guys so heres another debug nightmare that I've come across and can't seem to figure out exactly what is going on here -_-

Basically what is happening is that this code returns JSON to my ajax request. Which involves originally an array from the php.

array(
    "public_html"=> 
        array(
            "public_html"=>
                array(//...other files actually in public html),
        ),
    0 => 
        array(
            "filename":".ftpquota",
            "filesize": 12kb                         
        ),
);

So as you can see it's creating a public_html index then adding another with the same name with the actual files inside public_html this is not what I want, I want the original public_html to come back as the files in public_html

/*
  Iterates over files and directories recursively
*/
function ftpFileList($ftpConnection, $path="/") {
   //create general array to pass back later
   $files = array();

   //grabs the contents of $path
   $contents = ftp_nlist($ftpConnection, $path);

   //explode the $path to get correct index to fill in
   $secondaryPath = explode('/',$path);

   //test if the last index in the array is blank
   //if it is pop it from the array
   if($secondaryPath[count($secondaryPath) - 1] == "")
     array_pop($secondaryPath);
    //if the array is larger than or equal to one
    if(count($secondaryPath) >= 1){

     //we will rewrite $secondaryPath to the last index (actual file name)
     $secondaryPath = $secondaryPath[count($secondaryPath) - 1];
     }else{

     //if it isn't anything we'll make it the path that we originally passed
     $secondaryPath = $path;
   }

   //check for contents
   if($contents){
   //iterate over the contents
   foreach($contents as $currentFile) {

     //if the current file is not . or .. we don't need that at all
     if($currentFile !== "." && $currentFile !== ".."){
         //if there is a period in the currentFile this means it's a file not a directory
        if( strpos($currentFile,".") == 0 ) {

          if($files[""]){
              if($secondaryPath == $currentFile)
               $files[""][$secondaryPath][] = ftpFileList($ftpConnection, $path.$currentFile.'/');
              else
               $files[""][$secondaryPath][$currentFile] = ftpFileList($ftpConnection, $path.$currentFile.'/');
            }else{
               $files[$secondaryPath][] = ftpFileList($ftpConnection,$path.$currentFile.'/');
          }

      }else{
         //here we know the $currentFile is a file
         if($currentFile !== "." && $currentFile !== ".."){
             //file in the correct index with a new index which is an array with information
             $files[$secondaryPath][] = array(
                                "file"=>$currentFile,//file name
                                "filesize"=>humanFileSize(ftp_size($ftpConnection,"/".$path.$currentFile)),//human readable file size
                                "creation_date"=>date("F d Y g:i:sa",ftp_mdtm($ftpConnection,"/".$path.$currentFile)),//date in a human readable format
                                "full_path"=>"/".$path.$currentFile//full path of the file so we can access this later
                                );
            }
         }
      }
   }
   return $files;//return the array
  }
 }

I'm hoping this is partially easy to understand, I made a bunch of notes for debugging purposes. I need to iterate and get the files correctly in a nice JSON file so I can iterate over that with JS. It's hard to debug this with the internet I have and testing it in the IDE I am using, I only have a chromebook. (if anyone knows of a good app to use for the chromebook. )

0

There are 0 answers