从服务器上的文件位置下载损坏的文件

downloads corrupt files from the file location on server

文件移动到所需位置并在文件夹中打开但是当我下载它时下载了一个损坏的文件

我已经尝试了现有帖子中的所有建议,但似乎并不奏效。

<?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."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$name."&nbsp;&nbsp;&nbsp;&nbsp;<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);

}

?>

请在'set_time_limit(0);'之后和'readfile($path);'之前添加这两行代码,因为这些功能对于删除不必要的数据或防止损坏是必需的。

找到下面要添加的代码:

ob_clean();
flush();

希望这能解决您的问题。