How to Determine File Size of Files Over 2 GB in PHP on x86 Systems?

Mary-Kate Olsen
Release: 2024-10-21 16:35:30
Original
650 people have browsed it

How to Determine File Size of Files Over 2 GB in PHP on x86 Systems?

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!