简体   繁体   中英

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. 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:

  1. it's not connecting
  2. the filename/path ( $server_file ) is not correct

I made sure it's connected, the second problem might be possible. Where does $server_file come from? from my document root?

$server_file is the file you want to download from your server. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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