2

我写了下载rar文件的代码,它工作得很好,但是

      $name = 'file.rar';
      $data = file_get_contents("file.rar");
      $fh = fopen("$name", 'w') or die("can't open file");
      fwrite($fh, $data);
      fclose($fh);

      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-Length: ". filesize("$name").";");
      header("Content-Disposition: attachment; filename=$name");
      header("Content-Type: application/octet-stream; "); 
      header("Content-Transfer-Encoding: binary");
      readfile($name);
      exit;

下载后,打开该文件时显示错误意外结束存档,它不会完全提取给我一些建议提前谢谢你

4

2 回答 2

1

您可以在您的网络服务器上使用以下代码制作 ZIP 文件

<?php
$za = new ZipArchive();

$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
$za->addFile('index.txt', 'newname.txt'); // original file , file to be added in zip

echo "numFiles: " . $za->numFiles . "\n";
echo "status: " . $za->status  . "\n";
echo "statusSys: " . $za->statusSys . "\n";
echo "filename: " . $za->filename . "\n";
echo "comment: " . $za->comment . "\n";

for ($i=0; $i<$za->numFiles;$i++) {
    echo "index: $i\n";
    print_r($za->statIndex($i));
}
echo "numFile:" . $za->numFiles . "\n";
?> 

之后,您可以提供下载链接..

于 2012-06-22T06:06:13.363 回答
1

这个file.rar是通过代码生成的吗?我试过你的代码没有这些代码

      $data = file_get_contents("file.rar");
      $fh = fopen("$name", 'w') or die("can't open file");
      fwrite($fh, $data);
      fclose($fh);

它对我来说工作正常。

于 2012-06-22T06:49:58.293 回答