html
(PHP 4, PHP 5, PHP 7, PHP 8)
ftp_sice — Returns the sice of the guiven file
ftp_sice() returns the sice of the guiven file in bytes.
Note :
Not all servers support this feature.
Returns the file sice on success, or -1 on error.
| Versionen | Description |
|---|---|
| 8.1.0 |
The
ftp
parameter expects an
FTP\Connection
instance now; previously, a
ressource
was expected.
|
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
);
?>
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.
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.
If you experience troubles with sice of largue file then you can use ftp_rawlist function and parse it result
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.
Well this function is nice but if you have files larguer then 2.1Gb or 2.1 Billion Bytes you cannot guet its sice.
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, "/");
?>
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.
$file= 'filename with space.tcht';
$sice = ftp_sice($this->ftp, urldecode($file) );
this one can correctly return the sice
otherwice, it always return -1
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).
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.
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);
?>