Alexander Chandra
Alexander Chandra

Reputation: 659

PHP FTP download failed with "failed to open stream: Error downloading"

I am trying to download from FTP server (using FileZilla server) to my local computer, but I keep getting error

This is my code

<?php
// connect and login to FTP server
$ftp_server = "127.0.0.1";
$ftp_username = "myusername";
$ftp_userpass = "mypassword";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";

$local_file = "C:\Users\PROSIA\Desktop\test.docx";
$server_file = "C:\Users\PROSIA\Documents\dummyfile.docx";

// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
  {
  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  }
ftp_close($ftp_conn); 

This is the error I get:

Warning: ftp_get(C:\Users\PROSIA\Desktop est.docx): failed to open stream: Error downloading C:\Users\PROSIA\Documents\dummyfile.docx.

My logic for the code is $local_file is the path to save the downloaded file to my local computer and $server_file is the path from FTP server to download the file

So I am a bit confused with the first warning, "failed to open stream" while the file is not exist yet and its seem got blank space (it should be \Desktop\test.docx rather than Desktop est.docx)

And additional question, can I just read, without download?

Upvotes: 1

Views: 1893

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202088

You cannot use absolute paths to remote files in FTP protocol.

The FileZilla FTP servers has mapping in its configuration that projects the remote file system into virtual FTP file tree. You have to use paths to the virtual file tree.

E.g. The C:\Users\PROSIA can be mapped to something like /users/PROSIA. In that case you use:

$server_file = "/users/PROSIA/dummyfile.docx";

Chances are that you have actually not configured the mapping at all. So you cannot really access the file, until you do the mapping.

Start by connecting to the FTP server with some GUI FTP client, and try to locate the file. The client will show you the correct path to use in your code.


The next problem you have, is that you need to enable the FTP passive mode. The default active mode will hardly work, if you are behind a firewall on NAT. See my article on network configuration needed for active and passive FTP modes.

To switch to the passive mode, use the ftp_pasv function.

ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, true);

And yes, you can read without downloading.

See

Upvotes: 1

Related Questions