doushou8730 2017-02-17 06:20
浏览 99
已采纳

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

The file moves to desired location and opens in the folder but when i download it downloads a corrupt file

I have tried all the suggestions from the existing posts but it doesn't seem to do the trick.

<?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>

The upload code is:

<?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>

The download code is:

<?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);

}

?>
  • 写回答

1条回答 默认 最新

  • dpswo40440 2017-02-17 06:42
    关注

    Please add these two lines of code after 'set_time_limit(0);' & before 'readfile($path);', because these functions are necessary for removing unnecessary data or preventing from corruption.

    Find the code below, which are to be added:

    ob_clean();
    flush();
    

    I hope, this will solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记