从服务器php下载文件

编程入门 行业动态 更新时间:2024-10-24 00:23:47
本文介绍了从服务器php下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个URL,用于保存工作中的某些项目,它们大部分是MDB文件,但也有一些JPG和PDF.

I have a URL where I save some projects from my work, they are mostly MDB files, but some JPG and PDF are there too.

我需要做的是列出该目录中的每个文件(已经完成),并为用户提供下载文件的选项.

What I need to do is to list every file from that directory (already done) and give the user the option to download it.

使用PHP如何实现?

推荐答案

要读取目录内容,可以使用 readdir()并使用脚本(在我的示例中为download.php)下载文件

To read directory contents you can use readdir() and use a script, in my example download.php, to download files

if ($handle = opendir('/path/to/your/dir/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { echo "<a href='download.php?file=".$entry."'>".$entry."</a>\n"; } } closedir($handle); }

在download.php中,您可以强制浏览器发送下载数据,并使用 basename()进行确保客户端不会传递其他文件名,例如../config.php

In download.php you can force browser to send download data, and use basename() to make sure client does not pass other file name like ../config.php

$file = basename($_GET['file']); $file = '/path/to/your/dir/'.$file; if(!file_exists($file)){ // file does not exist die('file not found'); } else { header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); // read the file from disk readfile($file); }

更多推荐

从服务器php下载文件

本文发布于:2023-05-28 06:53:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   文件   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!