Hello,

I have a few scripts now reading files from an FTP, etc. My last step was to move the file from the FTP site, into a directory on the WebServer.

$ftp_server = "IP";
$ftp_user_name = "UN";
$ftp_user_pass = "PW";

// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$local_file = "abc.txt";
$server_file = "abc.txt";
ftp_pasv($conn_id, true); 
	if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII)) {
		echo "Successfully written to $local_file\n";
	} else {
    echo "There was a problem\n";
	}
ftp_close($conn_id);

My output is:

Warning: ftp_get(abc.txt) [function.ftp-get]: failed to open stream: Permission denied in /home/guest/www_root/ftp_get.php on line 6

Warning: ftp_get() [function.ftp-get]: Error opening bbpt.txt in /home/guest/www_root/ftp_get.php on line 6
There was a problem

I've tried a variety of local_file and server_file with the full file path as well. I moved the abc file to the root directory of the username/password on the ftp site instead of in the real path where I want it.

Not quite sure where I'm going wrong... Any ideas where my file permission error is? Thanks!

EDIT: I should point out I really have an include file in there for the connection, so line 6 is really the ftp_get() line.

    Does the user you're logging into the FTP with have the correct permissions? In this case, write since it's a linux based FTP.

      rsmith;10917092 wrote:

      Does the user you're logging into the FTP with have the correct permissions? In this case, write since it's a linux based FTP.

      Yes, I believe so. I can manually FTP to the directory, read the file, and write to the file as the same user. I even chmod 777'd the file to be sure.

        Do you mean on the FTP or Web server? How do I find out the user that would be doing the write on the Webserver? Thanks.

        EDIT:

        I checked a few things on the web server as your question prompted me to look at some additional settings:

        My envvars file:
        export APACHE_RUN_USER=www-data
        export APACHE_RUN_GROUP=www-data
        export APACHE_PID_FILE=/var/run/apache2.pid

        So I changed the permissions on the /home/guest/ directory to have a group of www-data:

        guest@box:/home$ ls -all
        total 16
        drwxr-xr-x  4 root  root    4096 May 19 19:28 .
        drwxr-xr-x 23 root  root    4096 May 21 19:32 ..
        drwxr-xr-x 32 guest guest   4096 Jun  4 02:07 guest
        guest@box:/home$ sudo chgrp -R www-data guest
        [sudo] password for guest: 
        guest@box:/home$ ls -all
        total 16
        drwxr-xr-x  4 root  root     4096 May 19 19:28 .
        drwxr-xr-x 23 root  root     4096 May 21 19:32 ..
        drwxr-xr-x 32 guest www-data 4096 Jun  4 02:07 guest
        

        Same output though...

          Aaah, I changed the owner, instead of the group and I wrote the file. Interesting. I'll have to revisit my permissions on this setup. Thanks for making my mind think 🙂

            Write a Reply...