通过网站从PHP服务器下载文件 [英] Downloading a file from a PHP server via a website

查看:101
本文介绍了通过网站从PHP服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些物理文件,希望用户可以在我的网站上下载。文件位于:

I have physical files which I want users to download on my website. The files are located at:

C:/ xampp / htdocs / myfile / uploads / *

我需要一个PHP脚本,该脚本可以在单击时动态下载文件。假设我有以下按钮,单击该按钮会触发 magic.php 脚本。

I need a PHP script which can download files dynamically on click. Let's say I have the following button which when clicked it triggers magic.php script.

< a href =" magic.php?file = name">下载文件< / a>

我需要在 magic.php 中下载哪些PHP代码我在查询参数中传递的文件名?

What PHP code do I need in magic.php to download the file name I passed in the query parameters?

推荐答案

下载链接

<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>

magic.php页面

magic.php page

<?php
$file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']);

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
?>

这篇关于通过网站从PHP服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆