0

i have a php script that should take a file from one web server and upload it on another, when i run the script through a browser it works fine. when i use this cronjob it doesn't upload.

51 14 * * * /usr/bin/php /kunden/homepages/25/d515048961/htdocs/FTP_Orders.php

is there something i am missing? the times are arbitrary, i am just testing it out.

the PHP script is:

<?php

// FTP access parameters
$host = 'paperlinx.co.uk';
$usr = '[email protected]';
$pwd = '######';

// file to move:
$local_file = './db_export_orders_2014-10-27.csv';
$ftp_path = '/exported/db_exported_orders_2014-10-27.csv';

// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");

// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");

// turn on passive mode transfers (some servers need this)
ftp_pasv ($conn_id, true);

// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);

// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n"; 

/*
** Chmod the file (just as example)
*/

// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) { 
//print "in not function exists";
   function ftp_chmod($ftp_stream, $mode, $filename){
        return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename)) or die ("i'm stuck here");
   }
} 

/* else

// try to chmod the new file to 666 (writeable)
 if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) 
{    
    print $ftp_path . "chmoded successfully to 666\n";
} 

else 
{

    print "could not chmod $file\n";
}
*/



// close the FTP stream
ftp_close($conn_id);

?>

I'm really stuck with this so any help would be greatly appreciated.

6
  • have you simply tried running above command in your shell? (/usr/bin/php ...)
    – vautee
    Nov 3, 2014 at 10:03
  • remove your password from the code!
    – JohnKiller
    Nov 3, 2014 at 10:26
  • 1
    Use full path on $local_file
    – jherran
    Nov 3, 2014 at 10:44
  • school boy error on the password, i had this question on stack overflow and got no response at all so thanks for the help so far! Nov 3, 2014 at 13:32
  • What is the exact error message that you're getting? What line of code is it executing when it produces the error?
    – Kenster
    Nov 3, 2014 at 14:20

1 Answer 1

0

The problem is that when you run the script from the cron job, the current working directory isn't the same as when you run it from the browser, so relative paths should be avoided.

Replace your $local_file with the full path, and it should work.

2
  • Thanks for you explanation on this, i have put the full path for the local file but i still get the 'cannot upload' error. i'm starting to go a little bit insane from this one! Nov 3, 2014 at 14:04
  • can you try this stackoverflow.com/a/12910495/2633161 ? php has no error messages for the FTP api and this seems to be the only way to see what's happening here
    – JohnKiller
    Nov 3, 2014 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .