简体   繁体   English

运行脚本时无法从FTP服务器自动下载文件

[英]Unable to make a file auto-download from an FTP server when script is run

I am attempting to write a PHP page that takes the GET variable which is a filename within the FTP and download it. 我正在尝试编写一个PHP页面,该页面接受GET变量,该变量是FTP中的文件名,并下载它。 However, it does not seem to work. 但是,它似乎不起作用。 The Function itself (ftp_get) is returning as TRUE as the echo statement is being run, however nothing else happens and there is no errors in console. 当运行echo语句时,函数本身(ftp_get)返回的是TRUE,但是没有其他反应,并且控制台中没有错误。

<?php  
$file = $_GET['file'];

$ftp_server = "127.0.0.1";
$ftp_user_name = "user";
$ftp_user_pass = "pass";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_get($conn_id, $file, $file, FTP_BINARY)) {
    echo "Successfully written to $file\n";
} else {
    echo "There was a problem\n";
}

?>

Ideally, I would simply link them to: ftp://example.com/TestFile.txt and it would download the file for them, however, it only shows them the contents of the file in their browser rather than downloading it. 理想情况下,我只需将它们链接到: ftp : //example.com/TestFile.txt ,它将为他们下载文件,但是,它只会在浏览器中向他们显示文件的内容,而不是下载文件。

I've gone through the PHP Manual site reading the FTP functions and I do believe ftp_get is the correct one I'm suppose to be using. 我已经遍历PHP手册站点,阅读了FTP函数,而且我确实相信ftp_get是我应该使用的正确函数。

Is there potentially an easier way of doing this, or is it just something I'm overlooking? 是否有可能更容易做到这一点,或者这只是我所忽略的事情?

There are two (or maybe more) ways you can do this. 您可以通过两种(或更多种)方法来执行此操作。 You could store a copy of the file on your server like you do with ftp_get and send it to the user afterwards. 您可以像使用ftp_get一样在服务器上存储文件的副本,然后将其发送给用户。 Or you could download it every time. 或者您可以每次下载它。

Now you can do this using ftp commands, but there is a quicker way using readfile . 现在您可以使用ftp命令执行此操作,但是有一种使用readfile的更快方法。
Following the first example from the readfile documentation: 遵循readfile文档中的第一个示例:

// Save the file as a url
$file = "ftp://{$ftp_user_name}:{$ftp_user_pass}@{$ftp_server}" . $_GET['file'];

// Set the appropriate headers for a file transfer
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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($file));
// and send them
ob_clean();
flush();

// Send the file
readfile($file);

This will simply fetch the file and forward it's contents to the user. 这将简单地获取文件并将其内容转发给用户。 And the header will make the browser save the file as a download. 标头将使浏览器将文件另存为下载文件。

And you could take this even further. 您甚至可以更进一步。 Let's say you save this in a file called script.php in a directory accessible to the user via http://example.com/ftp/ . 假设您将其保存在名为script.php的文件中,该文件可通过http://example.com/ftp/用户访问。 If you are using apache2 and have mod_rewrite enabled you can create a .htaccess file in this directory containing: 如果您使用的是apache2并启用了mod_rewrite,则可以在此目录中创建一个.htaccess文件,其中包含:

RewriteEngine On
RewriteRule ^(.*)$ script.php?file=$1 [L]

When the user navigates to http://exmaple.com/ftp/README.md your script.php file will be called with $_GET['file'] equal to /README.md and the file from ftp://user:pass@ftp.example.com/README.md will be downloaded on his computer. 当用户导航到http://exmaple.com/ftp/README.md将使用$_GET['file']等于/README.md的文件和来自ftp://user:pass@ftp.example.com/README.md的文件来调用script.php文件ftp://user:pass@ftp.example.com/README.md将被下载到他的计算机上。

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

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