简体   繁体   English

无法使用PHP从我的文件夹下载文件

[英]Unable to download my files from my folder using PHP

I'm currently using localhost to run my pages and currently I am trying to download the files my users have uploaded and stored in a folder called uploaded_files 我当前正在使用localhost来运行我的页面,并且当前正在尝试下载用户已上传并存储在名为Uploaded_files的文件夹中的文件

this is the code for my download page which isn't working and I'm not quite sure what's wrong. 这是我的下载页面的代码,该代码无法正常工作,我不太确定出了什么问题。

<?php

$file = $_GET['file'];

if (file_exists($file)) {
header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($name));
    ob_clean();
    flush();
    readfile("C:\xampp\htdocs\FYPproject\uploaded_files/".$file); //showing the path to the server where the file is to be download
    exit;
} else {
    echo "Download failed";
    echo $file;
}
?>

file_exists() need to receive the path of the file, if the file name is "test" and it's in "uploaded_files/" so you need to check file_exists("uploaded_files/test") . 如果文件名是"test"并且在"uploaded_files/" ,则file_exists()需要接收文件的路径,因此您需要检查file_exists("uploaded_files/test") You don't need to pass the whole path if your php is already in "FYPproject" folder. 如果您的php已经在"FYPproject"文件夹中,则无需传递整个路径。 It's the same for downloading files, you don't need to pass the whole path for readfile() . 下载文件是相同的,您不需要传递readfile()的整个路径。

You are passing a file path with backward slashes and forward slashes to readfile . 您正在将带有反斜杠和正斜杠的文件路径传递给readfile Even if this is not the cause of your problem you should change it. 即使这不是造成问题的原因,也应更改它。 Change it to this: 更改为此:

readfile("C:\\xampp\\htdocs\\FYPproject\\uploaded_files\\".$file);

并非所有浏览器都支持application/force-download因此在这种情况下,请尝试用application/octet-stream替换上面的代码,看看是否可行

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

相关问题 PHP代码可从文件夹中下载.pdf和.docx文件,并将其存储在Wamp Server中 - PHP code to download .pdf & .docx files from folder stored my Wamp Server 使用PHP将所有文件从SFTP文件夹下载到本地文件夹 - Download all files from SFTP folder to local folder using PHP 从我的网站将目录中的所有文件下载到Zip文件夹中 - Download all Files in a Directory from my Website into a Zip Folder 从wamp服务器下载图像,并使用php将其保存在我的文件夹中 - download image from wamp server and save in my folder loacated in same using php 从downloadlink下载pdf到我的上载文件夹PHP - Download pdf from downloadlink to my uploads folder PHP 无法使用PHP在Safari上下载我的CSV文件 - Unable to download my CSV file on Safari using PHP 使用 PHP 将一系列 Google 文档(.csv 文件)下载到我的网站 - Download a series of Google Docs (.csv files) to my site using PHP 使用PHP下载文件,我的下载停止在11MB - Downloading Files my download stops at 11MB using PHP PHP Zip并从多个文件夹下载文件 - PHP Zip and download files from multiple folder 如何防止用户使用用于在 Web 应用程序中下载 pdf 文件的 PHP 下载选项下载源代码文件? - How to prevent users from downloading source code files using the PHP download option I have for downloading pdf files in my web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM