note 101883 modified in function.filesize by danbrown
| From: | danbrown@php.net | Date: | Sun, 16 Jan 2011 17:56:52 +0000 |
| Subject: | note 101883 modified in function.filesize by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175800@lists.php.net to get a copy of this message | ||
I have a cli script running that use the filesize function on a ssh2_sftp connection. It has the
>2Gb limit issue, while it does not have that issue locally. I have managed to get around this by
doing a "du -sb" command through ssh2_shell.
The following function takes the ssh2_connect resource and the path as input. It may not be neat,
but it solves the problem for the moment.
<?php
function fSSHFileSize($oConn, $sPath) {
if(false !== ($oShell = @ssh2_shell($oConn, 'xterm', null, 500, 24,
SSH2_TERM_UNIT_CHARS))) {
fwrite($oShell, "du -sb '".$sPath."'".PHP_EOL);
sleep(1);
while($sLine = fgets($oShell)) {
flush();
$aResult[] = $sLine;
}
fclose($oShell);
$iSize = 0;
if(count($aResult) > 1) {
$sTemp = $aResult[count($aResult)-2];
$sSize = substr($sTemp, 0, strpos($sTemp, chr(9)));
if(is_numeric(trim($sSize))) {
$iTemp = (int)$sSize;
if($iTemp > "2000000000") $iSize = $iTemp;
}
}
return $iSize;
}
return 0;
}
?>
--was--
I have a cli script running that use the filesize function on a ssh2_sftp connection. It has the
>2Gb limit issue, while it does not have that issue locally. I have managed to get around this by
doing a "du -sb" command through ssh2_shell.
The following function takes the ssh2_connect resource and the path as input. It may not be neat,
but it solves the problem for the moment.
function fSSHFileSize($oConn, $sPath) {
if(false !== ($oShell = @ssh2_shell($oConn, 'xterm', null, 500, 24,
SSH2_TERM_UNIT_CHARS))) {
fwrite($oShell, "du -sb '".$sPath."'".PHP_EOL);
sleep(1);
while($sLine = fgets($oShell)) {
flush();
$aResult[] = $sLine;
}
fclose($oShell);
$iSize = 0;
if(count($aResult) > 1) {
$sTemp = $aResult[count($aResult)-2];
$sSize = substr($sTemp, 0, strpos($sTemp, chr(9)));
if(is_numeric(trim($sSize))) {
$iTemp = (int)$sSize;
if($iTemp > "2000000000") $iSize = $iTemp;
}
}
return $iSize;
}
return 0;
}
http://php.net/manual/en/function.filesize.php