ftp_size returns -1 even if it is not a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

ftp_size returns -1 even if it is not a directory

Post by revbackup »

Hi guys,

I am using ftp functions to delete files in an ftp server...

i have this code:

Code: Select all

 
$conn_id = ftpconnect($user,$password);
$dir = '/';
 
// Open a known directory, and proceed to read its contents
 
if($contents=ftp_nlist($conn_id, $dir)){
foreach($contents as $file){
           if (ftp_size($conn_id, $file) == -1) {
            $link ='/'.$user.'/';
            $path = str_replace(' ','%20',$file);
            
            //output as [ directory ]
             echo '
                      <tr id="'.$file.'" onmouseover="mouseover(this.id)" onmouseout="mouseout(this.id)"> 
                          <td height="21"><input name="checked[]" type="checkbox" value="'.$file.'">&nbsp;&nbsp;<img src="icons/folder.png">
                           <a href='.$link.$path.' class="style2">'.$file.'</a></td>
                          <td>---</td>
                          <td>---</td>
                     </tr>
                     ';
          }
        
   
   }//endforeach
 
 
   foreach($contents as $file){
   
           if (!(ftp_size($conn_id, $file) == -1)) {
                //get the image extension
               $img = explode('.',$file);
               //get the last modification date and time of the certain file
               $size = round(ftp_size($conn_id,$file)/1024)." Kb";//file size in kb
                if($size > 1000)
                {
                    $size = round($size/1024)." Mb";//file size in Mb
                }
               $lastmodified = date("d-M-Y H:i",ftp_mdtm($conn_id,$file));
               $link ='/'.$user.'/';
               $path = str_replace(' ','%20',$file);
                    echo '
                      <tr id="'.$file.'" onmouseover="mouseover(this.id)" onmouseout="mouseout(this.id)"> 
                          <td height="21"><input name="checked[]" type="checkbox" value="'.$file.'">&nbsp;&nbsp;<img src="icons/'.$img[1].'.png">
                           <a href='.$link.$path.' class="style2">'.$file.'</a></td>
                          <td>'.$size.'</td>
                          <td>'.$lastmodified.'</td>
                          <input name="dir" type="hidden" value="'.$dir.'">
                     </tr>
                     ';
          }//endif
   
   }//endforeach
}
else
{
echo 'Not a directory';
}
?>

Now, my server contains files like this:

/php [directory]
/scripts [directory]
/index.html [file]
/index1.html [file]
/upload.php [file]

If I would open directory '/', ftp_size would return
/php [-1]
/scripts [-1]
and the rest is read as a file which is good.....

the problem is, I want to remove the /php [directory] through
ftp_rmdir but it can't remove the directory because it is empty...
Now, when I go through the directory /php, it has subdirectories and files
within it:

/php/post/ [directory]
/php/put/ [directory]
/php/a.html [file]
/php/b.php [file]

when I do ftp_size through all those files, it would return -1 to ALL, including
/php/a.html
/php/b.php
which should be a file....

So that's my problem....

I tried to change the value of $dir which is '/' to '/php/' but the
result is, it would still return -1 for ALL even though it is not a directory...

that hinders me from deleting the non empty directory because ftp_size would
return a -1 even though it is a file, and not a directory...

What could be the problem???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: ftp_size returns -1 even if it is not a directory

Post by requinix »

It's quite possible that the FTP server doesn't support file sizes.
Try parsing the output from ftp_rawlist.

Umm... are you FTPing to the same server the PHP code is running on? Any particular reason for this?
Post Reply