【问题标题】:Download a zip file with PHP in a Mac computer在 Mac 电脑上用 PHP 下载一个 zip 文件
【发布时间】:2017-06-13 07:44:45
【问题描述】:

我正在尝试通过 PHP 创建和下载一个 zip 文件。除了在 mac 中,我的代码在所有可能的处置中都可以正常工作。当 mac 用户尝试下载文件时,浏览器会在几分钟后引发“空响应”错误。 zip 文件的大小是可变的,但总是接近 1.5Gb

这是启动 zip 创建和下载的 javascript 函数:

var peticion = ConstructorXMLHttpRequest();
if (peticion){
        disableButtons();
        peticion.open('POST', './php/createzip.php');
        peticion.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        peticion.onreadystatechange = function() {  //llama a una funcion cuando el estado cambia
            if(peticion.readyState == 4 && peticion.status == 200) {
                window.location.href = './php/download.php?az='+id_windturbine;
                enableButtons();
            }
        }

创建 zip 文件的代码(没有 bbdd 查询)是这个:

while($row = mysqli_fetch_row($result)){
         $url = '../'.$url1.$url2.'/'.$row[0];
         if ($gestor = opendir($url)){
            while (false !== ($entrada = readdir($gestor))){
                if (strpos($entrada,"full.png")!==false){
                    $files[] = $url.$entrada;
                }
            }
            closedir($gestor);
         }
    }

    $zip = new ZipArchive;
    $fecha = date('YmdGis');
    mkdir('../temp/'.$id_user.'_'.$fecha);
    $zipname = '/var/www/html/temp/'.$id_user.'_'.$fecha.'/images.zip';
    $zip->open($zipname,ZIPARCHIVE::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();
    mysqli_close($db);
    $_SESSION['zipaero'.$id_windturbine]=$zipname;

当这个php文件结束时,从js文件中可以看出,开始下载文件。

include('sesion.php');
    $aero=$_GET['az'];
    $zipname = $_SESSION['zipaero'.$aero];
    if(file_exists($zipname)){
        stream($zipname);
        unset($_SESSION['zipaero'.$aero]);
    }else{
        echo "El fichero $zipname no existe";
    }
    function stream($url){
        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename="images.zip"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($url));
        $archivo = fopen($url,"r");
        if (!($archivo===false)){
            while(!feof($archivo)){
                //imprimir contenido del archivo cada 1KB (kilobyte)
                print(fread($archivo, 1024));
                //decirle a apache que ya puede enviarlo
                ob_flush();
                flush();
            }
        }
        //cerrar el puntero al archivo
        fclose($archivo);
    }

有没有人遇到过同样的问题并且知道如何解决? 我认为问题是因为 createzip.php 脚本执行持续了很长时间(3 分钟)但我真的不知道如何解决它。

提前致谢

--- 使用新信息编辑---

我两次尝试了 Vivek D. 提供的代码。第一个下载开始了,但随后在 141Mb 处停止。

第二个提出了提到的空响应

如您在代码中所见,删除的 window.location.href 是 './php/download.php?az='+id_windturbine。 (捕获来自生产服务器)

我不知道这是否有帮助,但这是我用于 AJAX 调用的代码。

function ConstructorXMLHttpRequest()
{
    if(window.XMLHttpRequest) /*Vemos si el objeto window posee el metodo XMLHttpRequest(Navegadores como Mozilla y Safari).*/
    {
        return new XMLHttpRequest(); //Si lo tiene, crearemos el objeto
    }

    else if(window.ActiveXObject) /*Sino tenia el metodo anterior,deberia ser el Internet Exp.*/
    {
        var versionesObj = new Array(
                                    'Msxml2.XMLHTTP.5.0',
                                    'Msxml2.XMLHTTP.4.0',
                                    'Msxml2.XMLHTTP.3.0',
                                    'Msxml2.XMLHTTP',
                                    'Microsoft.XMLHTTP');

        for (var i = 0; i < versionesObj.length; i++)
        {
            try
                {
                    return new ActiveXObject(versionesObj[i]);
                }
                catch (errorControlado)
                {

                }
        }
    }
    throw new Error("No se pudo crear el objeto XMLHttpRequest");
}

【问题讨论】:

  • 您确定不是 Mac / PC 问题而是浏览器问题? Mac 的用户经常使用 Safari。也许您可以尝试使用 MAC + FF(或 Chrome)?
  • 您好,感谢您的回答。为了避免 Safari,我们要求用户在所有计算机中始终使用 chrome。我们还在 Mac 电脑上使用 chrome 测试此功能,但仍然出现错误。

标签: javascript php macos


【解决方案1】:

试试下面的代码。

function stream($url){
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: filename=image.zip');
        header("Content-Transfer-Encoding: Binary");
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($url));
        $archivo = fopen($url,"r");
        if (!($archivo===false)){
            while(!feof($archivo)){ 
                print(fread($archivo, 1024)); 
                flush();
            }
        }
        //cerrar el puntero al archivo
        fclose($archivo);
    }

【讨论】:

  • 请为您的答案添加解释。 为什么您的代码会解决问题?
  • 感谢您的回答。我刚刚尝试了你的代码两次,我得到了两个不同的错误。在第一个下载开始,但在 146mb 处停止。第二个提出了空响应错误。我将使用新信息编辑我的问题。
  • 您需要在php.ini文件中进行以下配置的更改: Memory_limit - 640M Max_execution_time - 1800 如果不起作用,则将执行时间增加到更高的值。
  • Memory_limit 已经是 4096Mb。 php 文件中的 max_execution_time 动态更改为 300。我将尝试 1800。
  • 它没有用。问题似乎出在创建 zip 文件的代码上。我试图在没有 AJAX 调用的情况下完成所有工作,并且是 zip 不起作用。
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2015-01-02
  • 1970-01-01
  • 2010-12-17
相关资源
最近更新 更多