简体   繁体   English

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

[英]List and download clicked file from 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.我有 FTP,我需要在上传目录中列出 FTP 中的所有文件,单击任何列出的文件后,它将从 FTP 下载特定文件。

It list my files in uploads directory but when I will try to download file so it types me "no file"它在上传目录中列出了我的文件,但是当我尝试下载文件时,它会输入“无文件”

My code:我的代码:

    // 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>";
}

Your link in the generated <a> tag points back to the web server, which does not contain the linked file.生成的<a>标记中的链接指向不包含链接文件的 Web 服务器。

What you need to do is to link to a PHP script, giving it a name of the file to download.您需要做的是链接到 PHP 脚本,为其指定要下载的文件的名称。 The script will then download the file from an FTP server and pass the downloaded file back to the user (to the webbrowser).然后脚本将从 FTP 服务器下载文件并将下载的文件传回给用户(到网络浏览器)。

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

A very trivial version of the download.php script: download.php脚本的一个非常简单的版本:

<?

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

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

The download.php script uses FTP URL wrappers . download.php脚本使用FTP URL 包装器 If that's not allowed on your web server, you have to go the harder way with FTP functions.如果您的 Web 服务器不允许这样做,则您必须更努力地使用 FTP 功能。 See PHP: How do I read a file from FTP server into a variable?请参阅PHP:如何将文件从 FTP 服务器读取到变量中?


Though for a really correct solution, you should provide some HTTP headers related to the file, like Content-Length , Content-Type and Content-Disposition .尽管对于真正正确的解决方案,您应该提供一些与文件相关的 HTTP 标头,例如Content-LengthContent-TypeContent-Disposition

Also the above trivial example will first download whole file from the FTP server to the webserver.同样,上面的简单示例首先将整个文件从 FTP 服务器下载到网络服务器。 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.什么是浪费时间和网络服务器上的内存。

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 .如需更好的解决方案,请参阅通过 PHP 脚本从 FTP 服务器将文件下载到带有 Content-Length 标头的浏览器,而无需将文件存储在 Web 服务器上

You may also want to autodetect Content-Type , unless all your files are of the same type.您可能还想自动检测Content-Type ,除非您的所有文件都属于同一类型。


A related question about implementing an FTP upload with a webpage:一个关于用网页实现FTP上传的相关问题:
Displaying recently uploaded images from remote FTP server in PHP用PHP显示最近从远程FTP服务器上传的图像

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM