【问题标题】:how to provide download link for already zipped files in php如何在php中为已经压缩的文件提供下载链接
【发布时间】:2013-02-06 07:29:43
【问题描述】:

我有用于创建一些文本文件的 zip 文件夹的 php 脚本

 <?php
  $path = "path\to\files";
  $files = array($path.'\ionic interaction.txt',$path.'\file1.txt',$path.'\file2.txt',$path.'\file3.txt',$path.'\file3.txt');
  $folder = $_POST['filename'];
  $zipname = $folder.'.zip';
  echo '<form action="download.php" method="post" name="zippass2download"><input    type="hidden" name="zipname" value="'.$zipname.'"></form>';
  $zip = new ZipArchive;
  $zip -> open($zipname, ZipArchive::CREATE);
  foreach($files as $file)
   {
     $zip -> addFile($file);
   }
  $zip -> close();
  ?>

在该脚本旁边,我创建了一个超链接,用于下载在第一个脚本中创建的 zip 文件夹。

 <a href="download.php">Download</a>

在 download.php 中我有以下代码

<?php
$path2zip = 'C:/wamp/www/PPInt';
$tobezipped = $_POST['zipname'];
header('Content-disposition: attachment; filename=$tobezipped');
header('Content-type: application/zip');
readfile('$path2zip/$tobezipped');
?>

不知道问题出在哪里。谁能帮我解决这个问题?

【问题讨论】:

  • 什么 echo '$variable';将会?见This
  • 我没有看到任何可以提交的内容&lt;form&gt;

标签: php zip


【解决方案1】:

$tobezipped = $_POST['zipname'];

未在调用 download.php 时发出的获取请求中设置。 Rember HTTP 是无状态的,所以在您生成文件之后。单击链接时,POST 参数不会出现在下一个 GET 请求中。

您可能需要将 zipname / 路径附加到您的 download.php 链接,如“download.php?tobezipped=zipname.zip”

【讨论】:

    【解决方案2】:

    “download.php”没有得到“$_POST['zipname']”,因为您没有将表单发布到它,而是提供了链接,请尝试提供用于发布您隐藏输入的表单的按钮文件名。 下面“Dasun”给出的代码的另一件事是检查您的文件和 zip 文件的相对 url。

    【讨论】:

      【解决方案3】:

      试试下面的代码

      <?php 
      $file_name = $_POST['zipname'];
      $file_url = 'C:/wamp/www/PPInt'. $file_name;
      header('Content-Type: application/octet-stream');
      header("Content-Transfer-Encoding: Binary"); 
      header("Content-disposition: attachment; filename=\"".$file_name."\""); 
      readfile($file_url);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2011-12-24
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        • 2011-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-09
        相关资源
        最近更新 更多