David
David

Reputation: 2232

php ftp_get() returns permission denied

I'm using a ftp upload script to transfer one server to another. It works fine in a browser window, and works great from an ssh command line by running: php /var/www/vhosts/domain.com/httpdocs/ftp_shell.php

but when I run the exact same command from a cron, I get this:

PHP Warning: ftp_get(ftp.tmp): failed to open stream: Permission denied in /var/www/vhosts/domain.com/httpdocs/ftp_shell.php on line 516 PHP Warning: ftp_get(): Error opening ftp.tmp in /var/www/vhosts/domain.com/httpdocs/ftp_shell.php on line 516

I looked on line 516, which is:

    if((ftp_get($ftp_from,"ftp".$tmp.".tmp",$fname,FTP_BINARY) ||
        ftp_get($ftp_from,"ftp".$tmp.".tmp",$fname,FTP_ASCII)) &&
       (ftp_put($ftp_to,$fname,"ftp".$tmp.".tmp",FTP_BINARY) ||
        ftp_put($ftp_to,$fname,"ftp".$tmp.".tmp",FTP_ASCII)) &&
       ftp_site($ftp_to,"chmod ".base_convert($chmod,10,8)." $fname") &&
       f_size()&&unlink("ftp".$tmp.".tmp")) {echoout(" (".$size_b." bytes) ok<br>");$count_f++;}

I know that it's writing the file to a temp file, but why would it allow me to do it in the browser and command line, but not a cron?

Upvotes: 4

Views: 5132

Answers (3)

JvdBerg
JvdBerg

Reputation: 21856

The error says:

PHP Warning: ftp_get(ftp.tmp): failed to open stream

and the line is:

if((ftp_get( $ftp_from, "ftp" . $tmp . ".tmp", $fname ...

so the error must be in a empty $tmp variable

Upvotes: 0

Probably the temp file was created by php script(apache user) and you want to read it from cron job (root user). Temp files are not readable from one users to others for security reasons.

You have to change your cron job to be executed by apache user, or you can alter the permissions of the temp file to be readable for everyone, but this is not recommended as any other procesess could read the file.

Upvotes: 2

Marc B
Marc B

Reputation: 360602

Jobs run under cron will have a different default working directory than whatever you're running from a webserver and/or the command line. Most likely whatever directory IS being used as the default while in cron does not have appropriate permissions for the cron job to use.

Try using absolute paths, e.g.

ftp_get($ftp_from,"/real/path/to/file/ftp".$tmp.".tmp",$fname,FTP_BINARY)
                   ^^^^^^^^^^^^^^^^^^^

instead of the relative paths you're using otherwise.

Upvotes: 1

Related Questions