简体   繁体   English

PHP下载后无法读取上传的文件

[英]Uploaded file is unable to read after downloading in PHP

I wrote php script to upload file from admin panel and to download for user.我编写了 php 脚本来从管理面板上传文件并为用户下载。 Uploading was done successfully but after downloading the file, it is unable to read.上传成功,但下载文件后无法读取。 if it is a image file or pdf file.如果是图像文件或pdf文件。 The uploaded files are copied to xamp/htdocs/myproject/admin/documents.上传的文件被复制到 xamp/htdocs/myproject/admin/documents。 and the path is stored to mysql database.并且路径存储到mysql数据库。 When i tried to download it the file size is different for uploaded file and downloaded file and it is unable to read.当我尝试下载它时,上传的文件和下载的文件的文件大小不同,并且无法读取。

The uploading script上传脚本

if (isset($_POST['submit'])){ 
 $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 posts (name,path) VALUES ('$location')");
}

Thanks everybody for helping me.谢谢大家帮助我。 i made changes for my script yesterday and it is working but the problem is that when i tried to add it to my project it is not working.我昨天对我的脚本进行了更改并且它正在工作,但问题是当我尝试将它添加到我的项目时它不起作用。

my uploads script(it is writen in myproject/admin/application.php)我的上传脚本(写在 myproject/admin/application.php 中)

if (isset($_POST['submit'])){
   $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 posts (name,path) VALUES ('$location')");   
}

download.php (Written in myproject/download.php) download.php(写在myproject/download.php)

<?php
 include('includes/connect.php');
  if (isset($_GET['dow']))  {
     $path = $_GET['dow'];
      $res = mysqli_query("SELECT * FROM posts WHERE path = '$path'");

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($path) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path); //showing the path to the server where the file is to be download
exit;
   }


 ?>

The linking script to download.php b (written in myproject/viewprofile.php) download.php b 的链接脚本(写在 myproject/viewprofile.php 中)

<?php
  include('includes/connect.php');

  $sql = "SELECT * FROM posts";
  $res = mysqli_query($con, $sql);






    $path   = $row['post_path'];

     echo "<a href='download.php?dow=$path' class='btn btn-dark btn-theme-colored btn-flat mr-5' style='width:100%; margin-top:5px;'> DOWNLOAD RESUME </a> <br>";

 ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM