简体   繁体   English

使用PHP从FTP下载文件

[英]Downloading file from FTP with PHP

I'm pretty new to FTP, and I'm trying to download a file to view it when it is saved local. 我对FTP不太熟悉,因此我尝试下载文件以在本地保存时查看。 But the first part is not going really smoothly... 但是第一部分进展并不顺利...

I used this code: 我使用以下代码:

        $conn_id = ftp_connect($ftp_server);

        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

        // define some variables
        $local_file = 'server.txt';
        $server_file = '/httpdocs/project/server/server.txt';

        // try to download $server_file and save to $local_file
        if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
            echo "Successfully written to this";
        } else {
            echo "There was a problem";
        }

But it isn't working, I don't know what the problem is. 但这不起作用,我不知道问题出在哪里。 Do I have to set some permissions or stuff? 我必须设置一些权限或东西吗? I couldn't figure out what was wrong so this is kind of my last hope. 我不知道出了什么问题,所以这是我最后的希望。

Where are my mistakes? 我的错误在哪里? Is it because of this code or is it something else? 是因为此代码还是其他原因?

Where are my mistakes? 我的错误在哪里? Is it because of this code or is it something else? 是因为此代码还是其他原因?

Your code is completly fine. 您的代码完全正确。 I know because I tested it. 我知道是因为我测试了它。

2 possible problems: 2个可能的问题:

  1. it's not connecting 它没有连接
  2. the filename/path ( $server_file ) is not correct 文件名/路径( $server_file )不正确

I made sure it's connected, the second problem might be possible. 我确保已连接,第二个问题可能是可能的。 Where does $server_file come from? $ server_file来自哪里? from my document root? 从我的文档根目录?

$server_file is the file you want to download from your server. $server_file是您要从服务器下载的文件。 Only you know the correct path. 只有您知道正确的路径。 Because we cannot tell which directory does the user have access to. 因为我们无法确定用户有权访问哪个目录。

Please check that /httpdocs/project/server/server.txt is the correct path of your file. 请检查/httpdocs/project/server/server.txt是文件的正确路径。

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

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