列出并从 FTP 下载点击的文件

List and download clicked file from FTP(列出并从 FTP 下载点击的文件)
本文介绍了列出并从 FTP 下载点击的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 FTP,我需要在上传目录中列出 FTP 中的所有文件,单击任何列出的文件后,它将从 FTP 下载特定文件.

I have FTP and I need list all files in FTP in uploads directory and after click on any listed file it will download the specific file from FTP.

它在上传目录中列出了我的文件,但是当我尝试下载文件时,它会键入无文件"

It list my files in uploads directory but when I will try to download file so it types me "no file"

我的代码:

    // connect and login to FTP server
$ftp_server = "IP";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "name", "password");

// list all files in upload directory and with "a" download file from FTP
$target_dir = "uploads/";
$files = ftp_nlist($ftp_conn, $target_dir);
foreach($files as $file) {
    echo "<a href='$file' download>$file</a>";
    echo "<br>";
}

推荐答案

生成的 <a> 标签中的链接指向不包含链接文件的 Web 服务器.

Your link in the generated <a> tag points back to the web server, which does not contain the linked file.

您需要做的是链接到一个 PHP 脚本,给它一个要下载的文件的名称.然后该脚本将从 FTP 服务器下载文件并将下载的文件传回给用户(给网络浏览器).

What you need to do is to link to a PHP script, giving it a name of the file to download. The script will then download the file from an FTP server and pass the downloaded file back to the user (to the webbrowser).

echo "<a href="download.php?file=".urlencode($file)."">".htmlspecialchars($file)."</a>";

download.php 脚本的一个非常简单的版本:

A very trivial version of the download.php script:

<?

header('Content-Type: application/octet-stream');

echo file_get_contents('ftp://username:password@ftp.example.com/path/' . $_GET["file"]);


download.php 脚本使用 FTP URL 包装器.如果您的网络服务器不允许这样做,您必须使用 FTP 功能更加困难.看PHP:如何将文件从 FTP 服务器读取到变量中?


The download.php script uses FTP URL wrappers. If that's not allowed on your web server, you have to go the harder way with FTP functions. See PHP: How do I read a file from FTP server into a variable?

虽然对于一个真正正确的解决方案,您应该提供一些与文件相关的 HTTP 标头,例如 Content-LengthContent-TypeContent-Disposition.

Though for a really correct solution, you should provide some HTTP headers related to the file, like Content-Length, Content-Type and Content-Disposition.

同样,上述简单示例将首先将整个文件从 FTP 服务器下载到网络服务器.只有这样它才会开始将其流式传输给用户(网络浏览器).什么是浪费时间和网络服务器上的内存.

Also the above trivial example will first download whole file from the FTP server to the webserver. And only then it will start streaming it to the user (webbrowser). What is a waste of time and also of a memory on the webserver.

要获得更好的解决方案,请参阅 通过 PHP 脚本将文件从 FTP 服务器下载到带有 Content-Length 标头的浏览器,而不将文件存储在网络服务器.

For a better solution, see Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server.

您可能还想自动检测 Content-Type,除非您的所有文件都属于同一类型.

You may also want to autodetect Content-Type, unless all your files are of the same type.

一个关于用网页实现FTP上传的相关问题:
用 PHP 显示最近从远程 FTP 服务器上传的图片

A related question about implementing an FTP upload with a webpage:
Displaying recently uploaded images from remote FTP server in PHP

这篇关于列出并从 FTP 下载点击的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)