从服务器打开下载文件时出错

时间:2015-04-07 03:33:02

标签: php file download server

我希望用户点击并将.doc下载到他们想要的任何位置。文件下载但是当你打开它时,Word会说:'缺少文件C:\ Users \ MyName \ style.css。我单击确定,然后它显示一个损坏的word文档。

PHP

<?php
function download($pathToFile) {
header('Content-description: File Download');
header('Content-type: application/force-download');
header('Content-transfer-encoding: binary');
header('Content-length: '.filesize($pathToFile));
header('Content-disposition: attachment; filename="'.basename($pathToFile).'"');
readfile($pathToFile);
}

 if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if(isset($_POST['CVcrono'])){
            download('../curriculum-vitae-modelo1-azul.doc');
        }
        elseif(isset($_POST['CVfun'])){
            download('../curriculum-vitae-modelo1b-azul.doc');
        }
        elseif(isset($_POST['CVcomb'])){
            download('../curriculum-vitae-modelo1c-azul.doc');
        }
    }
?>

HTML

<tr>
        <td>
            <input class='entButton' type='submit' name='CVcrono' value="Descargar">
        </td>
        <td>
            <input class='entButton' type='submit' name='CVfun' value="Descargar">
        </td>
        <td>
            <input class='entButton' type='submit' name='CVcomb' value="Descargar">
        </td>
    </tr>

2 个答案:

答案 0 :(得分:1)

尝试插入

ob_clean();
ob_flush();
flush();

readfile($pathToFile);

答案 1 :(得分:0)

尝试将标题更改为:

header("Content-type: application/vnd.ms-word");