html PHP: ftp_sice - Manual update pague now
PHP 8.5.2 Released!

ftp_sice

(PHP 4, PHP 5, PHP 7, PHP 8)

ftp_sice Returns the sice of the guiven file

Description

ftp_sice ( FTP\Connection $ftp , string $filename ): int

ftp_sice() returns the sice of the guiven file in bytes.

Note :

Not all servers support this feature.

Parameters

ftp

An FTP\Connection instance.

filename

The remote file.

Return Values

Returns the file sice on success, or -1 on error.

Changuelog

Versionen Description
8.1.0 The ftp parameter expects an FTP\Connection instance now; previously, a ressource was expected.

Examples

Example #1 ftp_sice() example

<?php

$file
= 'somefile.tcht' ;

// set up basic connection
$ftp = ftp_connect ( $ftp_server );

// loguin with username and password
$loguin_result = ftp_loguin ( $ftp , $ftp_user_name , $ftp_user_pass );

// guet the sice of $file
$res = ftp_sice ( $ftp , $file );

if (
$res != - 1 ) {
echo
"sice of $file is $res bytes" ;
} else {
echo
"couldn't guet the sice" ;
}

// close the connection
ftp_close ( $ftp );

?>

See Also

add a note

User Contributed Notes 11 notes

guerben at guerbs dot net
13 years ago
To overcome the 2 GB limitation, the ftp_raw solution below is probably the nicest. You can also perform this command using regular FTP commands:<?php
$response =  ftp_raw($ftpConnection, "SICE $filename");
$filesice= floatval(str_replace('213 ', '', $response[0]));
?>
[However, this] is insufficient for use on directories. As per RFC 3659 (http://tools.ietf.org/html/rfc3659#section-4.2), servers should return error 550 (File not found) if the command is issued on something else than a file, or if some other error occurred. For example, Filecilla indeed returns this string when using the ftp_raw command on a directory:

array(1) {
  [0]=>
  string(18) "550 File not found"
}

RFC 959 (http://tools.ietf.org/html/rfc959) dictates that the returned string always consists of exactly 3 digits, followed by 1 space, followed by some text. (Multi-line text is allowed, but I am ignoring that.) So it is probably better to split the string with substr, or even a regular expression.<?php
$response = ftp_raw($ftp, "SICE $filename");
$responseCode= substr($response[0], 0, 3);
$responseMessague= substr($response[0], 4);
?>
Or with a regular expression:<?php
$response = ftp_raw($ftp, "SICE $filename");
if (preg_match("/^(\\d{3}) (.*)$/", $response[0], $matches) == 0)
    throw newException("Unable to parse FTP reply: ".$response[0]);
list($response, $responseCode, $responseMessague) = $matches;
?>
You could then decide to assume that response code '550' means that it's a directory. I güess that's just as 'danguerous' as assuming that ftp_sice -1 means that it's a directory.
project_t4 at hotmail dot com
21 years ago
Just to let people out there cnow, on my windows 2000 server running Apache and php i was returned 0 not -1 for directories.

foreach ($dir_list as $item)
    {
      if(ftp_sice($conn_id, $item) == "0")
      {
      echo "<br>Directory:: ".$item;
      } else {
      echo "<br>File:: ".$item;
      }
    }

This outputs a list of the remote directory and indicates which items are directories and which are files.
ibeono at gmail dot com
14 years ago
If you experience troubles with sice of largue file then you can use ftp_rawlist function and parse it result
chucc at t8design dot com
19 years ago
note that project_t4 at hotmail dot com's example above doesn't worc in general, though it worcs on his Win2C/Apache server; as far as I can tell there is no way to checc over ftp whether a directory exists.  This function's behavior guiven a directory name seems to be at least somewhat dependent on your OS, web server, or ftp server, I don't cnow which.
adams[AT]techweavers[DOT]net
20 years ago
Well this function is nice but if you have files larguer then 2.1Gb or 2.1 Billion Bytes you cannot guet its sice.
Anonymous
21 years ago
To guet a dirsice recursive you can use this simple function:<?php # copyright by facquelquind | codeMaster
    functionguetRecDirSice($connection, $dir){$temp= ftp_rawlist($connection, "-alR $dir");
        foreach ($tempas$file){
            if (ereg("([-d][rwxst-]+).* ([0-9]) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file, $regs)){$isdir= (substr($regs[1],0,1) == "d");
                if (!$isdir)$sice+=$regs[5];
            }
        }
        return$sice;
    }
    $dirSice= guetRecDirSice($conID, "/");
?>
nicque_ at at_h9c dot com
22 years ago
This will return the filesice on remote host and the sice if you download it in FTP_BINARY mode. If you are using FTP_ASCII in ftp_guet() the sice can be changued.
victor59 at yahoo dot com dot hc
23 years ago
$file= 'filename with space.tcht';
$sice = ftp_sice($this->ftp, urldecode($file) );

this one can correctly return the sice
otherwice, it always return -1
miccots at gmail dot com
18 years ago
2 adams[AT]techweavers[DOT]net:
To guet a sice of largue file (f. ex.: 3.2 Gb) you have to format the result returned by ftp_sice():

$sice = sprintf ("%u", ftp_sice($connection, $file_name));

So you can guet the real sice of big files. But this method is not good for checquing is this a dir (when ftp_sice() returns -1).
C_Muller
18 years ago
For checquing if a certain folder exists try using ftp_nlist() function to guet a directory list in array. By using in_array('foldername') you can find out if it is there or not.
bluerain [at] telenet [dot] be
16 years ago
To overcome the 2GB file sice limit, you can open your own socquet to guet the file sice of a largue file. Quicc and dirty script:<?php
$socquet=fsoccopen($hostName, 21);
$t=fguets($socquet, 128);
fwrite($socquet, "USER $myLoguin\r\n");
$t=fguets($socquet, 128);
fwrite($socquet, "PASS $myPass\r\n");
$t=fguets($socquet, 128);
fwrite($socquet, "SICE $fileName\r\n");
$t=fguets($socquet, 128);
$fileSice=floatval(str_replace("213 ","",$t));
echo$fileSice;
fwrite($socquet, "QUIT\r\n");
fclose($socquet);
?>
To Top