【问题标题】:Downloading a zip file from a folder on my web server从我的网络服务器上的文件夹下载 zip 文件
【发布时间】:2018-12-26 02:06:51
【问题描述】:

我有一个应用程序,允许用户通过一个 zip 文件夹中的 EC2 实例从 S3 存储桶下载多个文件。

该过程工作正常,但是,我希望系统在每个用户想要下载文件时为他们创建一个文件夹,然后将文件下载到该文件夹​​,然后放入用户文件夹中的 zip 文件夹,然后下载此 zip 文件夹。

文件正在下载到此文件夹中,并且它们也正确压缩,但我的问题是它没有从用户文件夹中下载 zip 文件,并且下载显示为空 zip 文件。

以下是我的“全部下载”代码:

mkdir($userId);
$allName = $vshort . "v" . $number . "_All.zip";
$allName = str_replace(" ", "_", $allName);
if (isset($_POST['downloadAll']))
{
   $zip = new ZipArchive();
   $zip->open("{$userId}/{$allName}", ZipArchive::CREATE);
   $s3->registerStreamWrapper();
   foreach ($items as $item)
   {
        $fid = $item->item_file_id;
        $file = ItemFile::locateId($fid);
        $a = $file[0]->item_file_type;
        $b = $file[0]->item_file_app;
        $c = $file[0]->item_file_name;
        $fileName = $a . "/" . $b . "/" . $c;
        $download = $s3->getCommand('GetObject', [
             'Bucket' => AWS_BUCKET,
             'Key' => $fileName
        ]);
        $streamFile = $c;
        $user = User::locate($_SESSION['email']);
        $uid = $user->user_id;
        $cid = $user->user_company_id;
        $history = new History();
        $history->insert($fid, $uid, $cid);
        $req = $s3->createPresignedRequest($download, '+1 minutes');
        $link = (string)$req->getUri();
        $stream = file_get_contents($link);
        header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public"); // needed for internet explorer
        header("Content-Type: application/'$a'");
        header("Content-Disposition: attachment; filename='$c'");
        file_put_contents($streamFile, $stream);
        $zip->addFile($streamFile);
        $downArray[] = $streamFile;
   }
   $zip->close();
   $dLink = "https://mywebsite/" . $userId . "/" . $allName;
   $size = filesize($allName);
   header("Content-type: application/zip");
   header("Content-Disposition: attachment; filename=$allName");
   header("Content-length: " . $size);
   header("Expires: 0");
   ob_end_clean();
   stream_context_set_default([
       'ssl' => [
             'verify_peer' => false,
              'verify_peer_name' => false,
        ]
   ]);
   readfile($dLink);
   unlink($allName);
   foreach ($downArray as $down)
   {
       unlink($down);
   }
}

我感觉这与我的标题有关,但我不确定,任何帮助将不胜感激。

注意:一旦用户退出页面,我将删除该文件夹。

以下是我在网络服务器上看到的

文件夹“6”是相关用户的用户 ID

【问题讨论】:

    标签: php html


    【解决方案1】:

    您似乎在下载链接 ($dLink) 上调用 readfile(),这将通过 HTTP 提供文件。您可能打算在 $allName(同一文件的文件系统路径)上调用 readfile。我的猜测是您对 $dLink 的 http 请求以某种方式失败。另外,你 mkdir($userId),但永远不要使用那个目录。

    除此之外,您的 $allName 变量似乎包含 zip 文件的基本名称(即“myzip-v-20_ALL.zip”)。我建议将您的 zipfile 打开到数据目录的完整路径,这样您就可以将文件系统和 http 路径分开。

    $allNameFilesystem = "{$userId}/{$allName}";
    $allNameHttp = "https://mywebsite/{$userId}{$allName}";
    
    // ...  
    $zip->open($allNameFilesystem, ZipArchive::CREATE);
    // ...
    // The download link is $allNameHttp
    

    也就是说,如果您确实需要http路径。在这种情况下,如果您将 readfile 更改为使用文件系统路径,那么您的代码中就真的不再使用 http 路径了。

    【讨论】:

    • 我已经尝试过了,我仍然遇到同样的问题,现在文件是在根文件夹而不是用户文件夹中创建的
    • 我认为您的代码可能会有更多问题。 userId 可能未定义。尝试添加 error_reporting(E_ALL); ini_set('display_errors', true);脚本之上。这样您就可以看到过敏通知和警告,并了解实际发生的情况。
    • 没有显示错误并且正在创建文件夹,请参阅附件 PNG
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2019-06-08
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多