簡體   English   中英

PHP下載文件已損壞(從ftp服務器下載時)

[英]PHP downloading files has been corrupted(While downloading from ftp server)

我在從特定文件夾下載文件(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);
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;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM