简体   繁体   中英

PHP ftp script works in browser but not as CRON job?

I apologize in advance for posting this but I searched the forum but still couldnt find an answer.

My PHP script to create a csv and upload it to another ftp server works fine in the browser but doesn't work as a cron job. The cron job keeps sending me the "Error Uploading:", but when I run it on the browser everything works fine and I get "Successfully Uploaded:" I just can't figure this out. I'm on Godaddy fyi.

Cron Command:

*/2 * * * * /usr/bin/php5 -q -f /home/admin/public_html/cron.php

Here is my code:

$file_name = $_SERVER["DOCUMENT_ROOT"].'/path_to/file.csv';
$output = fopen($file_name, 'wb');

$query = 'SELECT * FROM table';
$result = mysqli_query($con, $query);

while($column = mysqli_fetch_field($result)){
   $columns[] = str_replace('_', ' ', $column->name);
}
fputcsv($output, $columns);

while ($field = mysqli_fetch_assoc($result)){
   fputcsv($output, $field);
}


$fileName = 'local_file.csv';
$file = $_SERVER["DOCUMENT_ROOT"].'/path_to/'.$fileName;
$remote_file = '/path_to/remote_file.csv';

$ftp_server = "domain.com";
$ftp_conn = ftp_connect($ftp_server);

$ftp_user = 'username';
$ftp_pass = 'password';
ftp_login($ftp_conn,$ftp_user,$ftp_pass);

if(ftp_put($ftp_conn, $remote_file, $file, FTP_BINARY)) {
   echo "Successfully Uploaded: $fileName.";

}
else {
   echo "Error Uploading: $fileName.";
}

ftp_close($ftp_conn);

cli php中没有$_SERVER["DOCUMENT_ROOT"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM