2016-05-09 18 views
1

特定のフォルダからファイル(php)をダウンロードする際に問題があります。ファイルをダウンロード中のPHPが壊れています(FTPサーバからダウンロード中)

ファイルをダウンロードして開くと、ファイルが破損していると表示されます。

アップロードされたファイルとダウンロードされたファイルのサイズを確認すると同じですが、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); 

答えて

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; 
      } 
+0

@Bekriを、それがダウンロードなっていない....私が知っていることができますしてください....これは私のコードの一部(上記)交換する必要があります – JMR

+0

私はedited.retryを持っています。 –

+0

if(file_exists($ path)){ 変更しました...... } –

関連する問題