PHP x86: Determining File Size of Files Exceeding 2 GB without External Programs
In PHP, retrieving the file size of files larger than 2 GB on 32-bit platforms poses a challenge. Standard PHP functions like filesize(), stat(), and fseek() fall short.
To address this, a comprehensive open-source project called Big File Tools emerged. It encompasses ingenious techniques to manage files over 2 GB in PHP, even on 32-bit systems.
One method employed by Big File Tools attempts to leverage platform-specific shell commands. For Windows, it utilizes shell substitution modifiers. For *nix and macOS, it employs the stat command. If these fail, it shifts to COM (specifically for Windows). As a last resort, it reverts to filesize().
Here's a code snippet showcasing how you can determine the file size of a large file using this approach:
<code class="php">function filesize64($file) { $size = null; // Try shell command if (function_exists('exec') && !ini_get('safe_mode')) { $cmd = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') ? "for %F in (\"$file\") do @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 (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && class_exists("COM")) { try { $fsobj = new COM('Scripting.FileSystemObject'); $f = $fsobj->GetFile(realpath($file)); $size = $f->Size; } catch (Exception $e) {} if (ctype_digit($size)) { return $size; } } // Fall back to filesize() return filesize($file); }</code>
The above is the detailed content of How to Determine File Size of Files Over 2 GB in PHP on x86 Systems?. For more information, please follow other related articles on the PHP Chinese website!