1

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?

Alexander Chandra
  • 587
  • 2
  • 9
  • 23

1 Answers1

1

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank you for your answer, the read file works fine but the download still not working, and then i try using fireftp(mozil addon) to download but its doesnt working too (maybe the privilege? i already check all (read,write,delete,append,create,delete,list,+subdir)) – Alexander Chandra Mar 10 '17 at 07:11
  • What do you mean by "read file"? Do you mean directory listing? What error do you get when downloading? Use some FTP client that can do logging and show us the log. – Martin Prikryl Mar 10 '17 at 07:13
  • I mean read file from ftp server using php. This is the error i get Warning: ftp_get(/users/PROSIA/Deskstop/test.docx): failed to open stream: No such file or directory in C:\xampp\htdocs\xxx.phtml and Warning: ftp_get(): Error opening /users/PROSIA/Deskstop/test.docx in C:\xampp\htdocs\xxx.phtml Error downloading /users/PROSIA/Documents/dummyfile.docx. – Alexander Chandra Mar 10 '17 at 07:31
  • Sorry, I do not understand. So what "works fine"? - Anyway, first make the download working using a standalone FTP client. No point trying PHP, until plain FTP works. – Martin Prikryl Mar 10 '17 at 07:32