【问题标题】:PHP downloading files has been corrupted(While downloading from ftp server)PHP 下载文件已损坏(从 ftp 服务器下载时)
【发布时间】:2016-05-09 18:23:02
【问题描述】:

我在从特定文件夹下载文件(php)时遇到问题。

当我下载并打开文件时,它说您的文件已损坏。

当我检查上传文件和下载文件的大小时,它是相同的,但对于 zip 文件的大小它不同。

没有文件正在打开。

谁能说我错在哪里???

if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
  $filename = $_GET['file'];
} 
else
{
  $filename = NULL;
}

$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
  echo $err;
} 
else 
{
// define the path to your download folder plus assign the file name
  $path = '/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
  if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
    $size = filesize($path);
    header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment;filename="'.basename($filename).'"');
    header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
    $file = @ fopen($path, 'rb');
    if ($file) {
// stream the file and exit the script when complete
      fpassthru($file);
      exit;
    } else {
      echo $err;
    }
  } else {
    echo $err;
  }

  exit;

}

插入表格:

 echo "<tr><td><a href='?file=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";

我很高兴文件已下载但未打开。

.txt 文件正在打开。

也检查了标题。

我试过把:

ob_clean();
flush();
readfile($file);

【问题讨论】:

    标签: php upload


    【解决方案1】:
    if (file_exists($path)) {
    
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename=' . basename($path));
                    header('Content-Transfer-Encoding: binary');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($path));
                    ob_clean();
                    flush();
                    readfile($path);
                    exit;
                }
    

    【讨论】:

    • @Bekri 它没有被下载....请我知道代码的确切位置....我的代码的哪一部分(上面)应该被替换
    • No Bekir 它没有下载,但在 url 我得到了文件名。
    • 虽然这可能会回答 OP 的问题,但在没有任何解释的情况下发布可靠的代码块没有任何教育目的。请通过解释为什么此代码是一个解决方案或者它是否基于 OP 的更改以及更改原因来改进您的答案。
    • @Bekir 同样的问题已损坏
    • 你把文件编码为utf-8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多