我写了php脚本来从管理面板上传文件并为用户下载。 上传成功完成但下载文件后无法读取。如果是图像文件或pdf文件。上传的文件将复制到xamp / htdocs / myproject / admin / documents。并将路径存储到mysql数据库中。当我尝试下载它时,上传文件和下载文件的文件大小不同,无法读取。
上传脚本
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')");
}
答案 0 :(得分:0)
感谢大家帮助我。我昨天对我的脚本进行了更改并且它正在运行,但问题是当我尝试将其添加到我的项目时它无效。
我的上传脚本(在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(写在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;
}
?>
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>";
?>