hey everyone im working on uploading stuff to a gallery. For uploading i did a chown group 770 for uploading secure stuff.

The Ftp user i login with has access to delete.
but im trying delete the file and iam always getting warning: ftp_delete access not aloud.

anyone have any idea?

    Okay as Iam doing more research on this, I know it has to do with permissions.

    When uploading. I login ftp_connect(). upload the files. ftp_disconnect();

    now when the user tries to delete i do the same exact thing same username and password login but I have issues with access denied when trying to delete. No one ever came across something like this or knows how to fix the problem? i even try doing ftp_chmod() but no effect......

      Okay from what i see i can delete files and directorys one by one.

      But i want to delete a folder that has files or folders in them so i ran across this:

      function ftp_rmdirr($ftp_stream, $directory)
              {
                  // Sanity check
                  if (!is_resource($ftp_stream) ||
                      get_resource_type($ftp_stream) !== 'FTP Buffer') {
      
                  return false;
              }
      
              // Init
              $i             = 0;
              $files         = array();
              $folders       = array();
              $statusnext    = false;
              $currentfolder = $directory;
      
              // Get raw file listing
              $list = ftp_rawlist($ftp_stream, $directory, true);
      
              // Iterate listing
              foreach ($list as $current) {
      
                  // An empty element means the next element will be the new folder
                  if (empty($current)) {
                      $statusnext = true;
                      continue;
                  }
      
                  // Save the current folder
                  if ($statusnext === true) {
                      $currentfolder = substr($current, 0, -1);
                      $statusnext = false;
                      continue;
                  }
      
                  // Split the data into chunks
                  $split = preg_split('[ ]', $current, 9, PREG_SPLIT_NO_EMPTY);
                  $entry = $split[8];
                  $isdir = ($split[0]{0} === 'd') ? true : false;
      
                  // Skip pointers
                  if ($entry === '.' || $entry === '..') {
                      continue;
                  }
      
                  // Build the file and folder list
                  if ($isdir === true) {
                      $folders[] = $currentfolder . '/' . $entry;
                  } else {
                      $files[] = $currentfolder . '/' . $entry;
                  }
      
              }
      
              // Delete all the files
              foreach ($files as $file) {
                  ftp_delete($ftp_stream, $file);
              }
      
      
              // Delete all the directories
              // Reverse sort the folders so the deepest directories are unset first
              rsort($folders);
              foreach ($folders as $folder) {
                  ftp_rmdir($ftp_stream, $folder);
              } 
              // Delete the final folder and return its status
              return ftp_rmdir($ftp_stream, $directory);
          }

      but when it tries to delete thats what i get the error/warning. Odd?

        Are you on shared hosting? Your provider may have disabled the ftp_delete() function for some reason.

        Edit: Apologies, didn't read your post carefully enough. You can delete single files so it's not ftp_delete() being disabled by the host.

          yea. not sure somethings up?

          i use this function now...it seems to work just wondering why the other code would not work(think its a little faster from what i read on the creators post)

          function ftp_delAll($conn_id,$dst_dir){
                  $ar_files = ftp_nlist($conn_id, $dst_dir);
                  //var_dump($ar_files);
                  if (is_array($ar_files)){ // makes sure there are files
                      for ($i=0;$i<sizeof($ar_files);$i++){ // for each file
                          $st_file = basename($ar_files[$i]);
                          if($st_file == '.' || $st_file == '..') continue;
                          if (ftp_size($conn_id, $dst_dir.'/'.$st_file) == -1){ // check if it is a directory
                              ftp_delAll($conn_id,  $dst_dir.'/'.$st_file); // if so, use recursion
                          }else ftp_delete($conn_id,  $dst_dir.'/'.$st_file); // if not, delete the file
                      }
                      sleep(1);
                      ob_flush() ;
                  }
                  $flag = ftp_rmdir($conn_id, $dst_dir); // delete empty directories
          
              return $flag;
          } // end of function ftp_delAll()
          
          
            a year later
            slick101;10916805 wrote:

            yea. not sure somethings up?

            i use this function now...it seems to work just wondering why the other code would not work(think its a little faster from what i read on the creators post)

            function ftp_delAll($conn_id,$dst_dir){
                    $ar_files = ftp_nlist($conn_id, $dst_dir);
                    //var_dump($ar_files);
                    if (is_array($ar_files)){ // makes sure there are files
                        for ($i=0;$i<sizeof($ar_files);$i++){ // for each file
                            $st_file = basename($ar_files[$i]);
                            if($st_file == '.' || $st_file == '..') continue;
                            if (ftp_size($conn_id, $dst_dir.'/'.$st_file) == -1){ // check if it is a directory
                                ftp_delAll($conn_id,  $dst_dir.'/'.$st_file); // if so, use recursion
                            }else ftp_delete($conn_id,  $dst_dir.'/'.$st_file); // if not, delete the file
                        }
                        sleep(1);
                        ob_flush() ;
                    }
                    $flag = ftp_rmdir($conn_id, $dst_dir); // delete empty directories
            
                return $flag;
            } // end of function ftp_delAll()
            
            

            Even though it's been almost a year since this post... All I have to say is this..

            It's about time!! Thank you very much for this!

            Any readers out there looking for a function to not only delete files recursively, but, this function does NOT cause any PHP errors / notices, etc.

            I have a database driven error reporting system and as you can imagine the log would build up really fast.

            The problem with most ftp_rmdir "recursive" functions out there is that they still cause errors even though the user does not see them, they are really there.

            Errors Like:

            ftp_rmdir() [function.ftp-rmdir]: Can't remove directory: Not a directory
            

            OR

            ftp_rmdir() [function.ftp-rmdir]: Can't remove directory: Directory not empty
            

            Anyway, I can finally go to bad after working on this for about 3 - 4 hours now.

            Thanks Again,

            Luke

              Write a Reply...