2016-05-09 5 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

Я отредактировал.retry. –

+0

если (file_exists ($ путь)) { Изменены ...... } –

Смежные вопросы