PHP非txt文件在下载时不会打开

时间:2018-02-25 10:40:13

标签: php file pdo

在我的网站上,我允许用户在填写表单时下载其他人发布的传记。我收到的问题是,当我打开一个下载的" txt"文件,它打开正常但是,当它像" docx或accdb"文件,我收到错误消息,如

We're sorry. We can't open xxx because we found a problem with its contents.
Details>>
The file is corrupt and cannot be opened

我相信当我从用户那里获取文件时会出现问题。这是我的代码

上传文件

$bio_name = $_FILES['biography']['name'];
 $bio_tmp = $_FILES['biography']['tmp_name'];
 $bio_size = $_FILES['biography']['size'];
 $bio_type = $_FILES['biography']['type'];
 $ext = pathinfo($bio_name, PATHINFO_EXTENSION);


                if($ext=="pdf"||$ext=="PDF"||$ext=="doc"||$ext=="DOC"||$ext=="docx"||$ext=="DOCX"
                ||$ext=="XLS"||$ext=="xls"||$ext=="XLSX"||$ext=="xlsx"||$ext=="xlsm"||$ext=="XLSM"){
$fp = fopen($bio_tmp, 'r');
$content = fread($fp, filesize($bio_tmp));
$content = addslashes($content);
fclose($fp);


 }else{


echo "<script>
alert('ERROR: This file format is not supported');
window.location.href='form.php';
</script>";
     }

下载档案

$size = $formData['bio_size'];
        $type = $formData['bio_type'];
        $file = $formData['bio_filename'];
        $content = $formData['bio_tmp'];
         header("Content-length: $size");
         header("Content-type: $type");
         header("Content-Disposition: attachment; filename=$file");
         ob_clean();
         flush();
         echo $content;

0 个答案:

没有答案