文件移动到所需位置并在文件夹中打开,但是当我下载它时会下载损坏的文件
我已经尝试了现有帖子中的所有建议,但它似乎没有做到这一点。
<?php
$error = "error";
$con = mysqli_connect('localhost','root','') or die($error);
mysqli_select_db($con, 'folder') or die($error);
$sql = "SELECT * FROM documents";
$res = mysqli_query($con, $sql);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a href="upload.php">Photo</a><br><br>
<?php
while ($row = mysqli_fetch_array($res)) {
$id = $row['id'];
$name = $row['name'];
$path = $row['path'];
echo $id." ".$name." <a href='download.php?dow=$path'>Download</a><br>";
}
?>
</body>
</html>
上传代码为:
<?php
$error = "error";
$con = mysqli_connect('localhost','root','') or die($error);
mysqli_select_db($con,'servicereport') or die($error);
if (isset($_POST['submit'])) {
$doc_name = $_POST['doc_name'];
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name) {
$location = "documents/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query($con,"INSERT INTO documents(name,path) VALUES ('$doc_name','$location') ");
header('location:photos.php');
}
else
die("please select a file");
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Name</label>
<input type="text" name="doc_name">
<input type="file" name="myfile">
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
下载代码为:
<?php
include('inc/db.php');
if (isset($_GET['dow'])) {
$path = $_GET['dow'];
$res = mysqli_query("SELECT * FROM documents WHERE path = '$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('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length:'.sprintf("%u", filesize($path)));
set_time_limit(0);
readfile($path);
}
?>
答案 0 :(得分:0)
请在'set_time_limit(0);'
&amp;之后添加这两行代码。在'readfile($path);'
之前,因为这些功能对于删除不必要的数据或防止损坏是必要的。
找到下面的代码,这些代码将被添加:
ob_clean();
flush();
我希望,这会解决你的问题。