PHP x86 How to get filesize of > 2 GB file without external program? -
i need file size of file on 2 gb in size. (testing on 4.6 gb file). there way without external program?
current status:
filesize()
,stat()
,fseek()
failsfread()
,feof()
works
there possibility file size reading file content (extremely slow!).
$size = (float) 0; $chunksize = 1024 * 1024; while (!feof($fp)) { fread($fp, $chunksize); $size += (float) $chunksize; } return $size;
i know how on 64-bit platforms (using fseek($fp, 0, seek_end)
, ftell()
), need solution 32-bit platform.
solution: i've started open-source project this.
big file tools
big file tools collection of hacks needed manipulate files on 2 gb in php (even on 32-bit systems).
here's method used in soloadmin (an old freebsd-licensed project).
it first attempts use platform-appropriate shell command (windows shell substitution modifiers or *nix/mac stat
command). if fails, tries com (if on windows), , falls filesize()
.
the original function can found here: index.php: line 1866 (svn)
i have posted modified version here, edited remove dependencies on other project-specific functions.
function filesize64($file) { static $iswin; if (!isset($iswin)) { $iswin = (strtoupper(substr(php_os, 0, 3)) == 'win'); } static $exec_works; if (!isset($exec_works)) { $exec_works = (function_exists('exec') && !ini_get('safe_mode') && @exec('echo exec') == 'exec'); } // try shell command if ($exec_works) { $cmd = ($iswin) ? "for %f in (\"$file\") @echo %~zf" : "stat -c%s \"$file\""; @exec($cmd, $output); if (is_array($output) && ctype_digit($size = trim(implode("\n", $output)))) { return $size; } } // try windows com interface if ($iswin && class_exists("com")) { try { $fsobj = new com('scripting.filesystemobject'); $f = $fsobj->getfile( realpath($file) ); $size = $f->size; } catch (exception $e) { $size = null; } if (ctype_digit($size)) { return $size; } } // if else fails return filesize($file); }
Comments
Post a Comment