在我的网站上,我从用户提交的数据库中获取特定文件。我得到的问题是,当我下载提交的文件时,我得到一个空的" docx或pptx或txt"文件。我尝试提交一个带有文本的文字doc,当我下载它时,我得到了相同的结果(一个空文件)。我相信问题在于我实际从用户检索文件并将其发送到数据库。这是我检索和下载文件的代码。
检索文件
$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" || $ext=="txt"||$ext=="TXT" || $ext=="pptx"||$ext=="PPTX"){
$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='speakerform.php';
</script>";
}
下载档案
$speaker_id = 13;
$stmtSpeaker = $handler->prepare("SELECT * FROM formdata WHERE user_id= :speaker_id");
$stmtSpeaker->bindParam(':speaker_id', $speaker_id, PDO::PARAM_INT);
$stmtSpeaker->execute();
$formData = $stmtSpeaker->fetch();
$size = $formData['bio_size'];
$type = $formData['bio_type'];
$file = $formData['bio_filename'];
$content = $formData['bio_tmp'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;
答案 0 :(得分:1)
哇!它工作得很好,请你给我发回答率。再次感谢@FunkFortyNiner - Jagr
根据要求并从我的评论中删除:
&#34;你的意思是我应该将提交的每个文件存储到文件目录中吗?并从那里检索它?&#34; - 就是这样。 (我)
上传文件https://secure.php.net/manual/en/features.file-upload.post-method.php的手册是您需要使用的。
以下是手册中的一个示例:
HTML:
<form enctype="multipart/form-data" action="__URL__" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
PHP
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
然后将其应用于您的代码,然后使用该引用上传文件,然后在提取查询语句中上传文件的引用。