Forum Moderators: coopster

Message Too Old, No Replies

ftp_size returns different filesize then IE

         

designaweb

6:31 pm on Sep 5, 2004 (gmt 0)

10+ Year Member



Hi,

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?

designaweb

6:48 pm on Sep 5, 2004 (gmt 0)

10+ Year Member



OK most likely it has something to do with checking the size in either BINARY or ASCII mode. However, there is no way to add this parameter in the ftp_size() function....

Is there a way to translate binary size into ascii size, and vice versa?

Adrian2k4

9:19 pm on Sep 5, 2004 (gmt 0)

10+ Year Member



what are the differences between ie and ftp_size?
could it be the *1024 conversion from bytes to kb etc.?
I.e. internet explorer gives you the filesize in kb and ftp_size in bytes?

Adrian2k4

9:27 pm on Sep 5, 2004 (gmt 0)

10+ Year Member



i found this on [php.net...]

"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

designaweb

10:20 am on Sep 6, 2004 (gmt 0)

10+ Year Member



Thanks for all the help. I also found the topics you mentioned. However, with ftp_size() you cannot set it to binary or ascii mode yet, they only discussed this. ftp_exec() does not work with our version of PHP (4.1.2 - has it's reasons). Therefore I cannot send a RAW command to change 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];

}

}

}