php file_get_contents():内容从2147483648截断到2147483647字节

4 php zip file memory-limit http-headers

当我要创建2GB文件的zip文件时,如何找出问题.

错误

file_get_contents():内容从2147483648截断到2147483647字节

致命错误:内存不足(分配2151677952)(试图分配18446744071562067968字节)

我正在使用专用服务器并已设置memory_limit,max_execution_time,max_upload_filesize,max_post_size.但它不适合我.请检查我的代码,让我知道我做错了什么 -

创建新的zip对象

    $zip = new ZipArchive();

    # create a temp file & open it
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);

    # loop through each file
    foreach($files as $file){
        # download file
        $download_file = file_get_contents($file_path.'/'.$file);
        #add it to the zip
        $zip->addFromString(basename($file_path.'/'.$file),$download_file);
    }

    # close zip
    $zip->close();
    $zip_name = $last_seg.'.zip';
    # send the file to the browser as a download
    header("Content-disposition: attachment; filename=$zip_name");
    header('Content-type: application/zip');
    readfile($tmp_file);
Run Code Online (Sandbox Code Playgroud)

Adr*_*uer 5

我改为$zip->addFromString(),$zip->addFile()因为你不需要阅读内容文件来添加文件,我用3部电影测试你的代码并且不起作用(我有同样的错误)但是当我使用$zip->addFile()all go ok,我可以下载zip文件与3GB.

我需要用 set_time_limit(0);

如果要测试此代码,只需更改以下值:

$files //Array of files name $file_path //Path where your files ($files) are placed $last_seg //The name of your zip file

<?php

    set_time_limit(0);

    $files = array('Exodus.mp4', 'the-expert.webm', 'what-virgin-means.webm');
    $file_path = 'zip';
    $last_seg = 'test';

    $zip = new ZipArchive();

    # create a temp file & open it
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);

    # loop through each file
    foreach($files as $file){
        $zip->addFile($file_path.'/'.$file, $file);
    }

    # close zip
    $zip->close();
    $zip_name = $last_seg.'.zip';
    # send the file to the browser as a download
    header("Content-disposition: attachment; filename=$zip_name");
    header('Content-type: application/zip');
    readfile($tmp_file);

?>
Run Code Online (Sandbox Code Playgroud)

您可以在以下网址阅读更多

http://php.net/manual/en/ziparchive.addfile.php