Forum Moderators: coopster
I am building a tool that amongst many other things can check the filesize of a file on a remote FTP server. The code I use for this is
// Get filename and path
$url_info = parse_url($url);
$file = $url_info['path'];
// set up basic connection
$conn_id = ftp_connect($url_info['host']);
// login with username and password
$login_result = ftp_login($conn_id, 'anonymous', 'anonymous');
// get the size of $file
$filesize = ftp_size($conn_id, $file);
if ($res!= -1) {
return $filesize;
} else {
echo "Couldn't get the size thru FTP";
}
// close the connection
ftp_close($conn_id);
This works really well, except for the fact that it returns a different bytesize then IE does. I want it to get the same size as IE reports...
Anyone?
"This [ftp_size] will return the filesize on remote host and the size if you download it in FTP_BINARY mode. If you are using FTP_ASCII in ftp_get() the size can be changed."
also you could try sending raw ftp commands with ftp_raw
Raw FTP command list:
[nsftools.com...]
Try SIZE after changing transfer type with TYPE
The solution for my problem was the following - not very nice, but it works:
// examine url and make an array out of it
$url_info = parse_url($url);
$url_info['file'] = array_pop(explode("/", $url_info['path']));
$url_info['dir'] = substr($url_info['path'], 0, -strlen($url_info['file']));
if ($url_info['scheme'] == 'ftp') {
// set up basic connection
$conn_id = ftp_connect($url_info['host']);
// login with username and password
$login_result = ftp_login($conn_id, 'anonymous', 'anonymous');
// get the binary size of $file
$filesize['binary'] = ftp_size($conn_id, $url_info['path']);
// loop thru ls -al list and get filesize of $file
$list = ftp_rawlist($conn_id, $url_info['dir']);
foreach($list as $key => $value) {
// if we find the file in $value
if (strstr($value, $url_info['file'])) {
// grab ascii the filesize
if (eregi('^[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+([0-9]+)[ ]+', $list[$key], $regs)) {
$filesize['ascii'] = $regs[1];
}
}
}