Jim
Jim

Reputation: 1315

FTP file upload using php

I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:

<?php
require_once('ftp.php');

// 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);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
    exit;
} else {
    echo "There was a problem while uploading $file\n";
    exit;
}
// close the connection
ftp_close($conn_id);
?>

ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:

There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv 

EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:

$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";

What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server? Thanks.

Upvotes: 2

Views: 5814

Answers (1)

bitWorking
bitWorking

Reputation: 12675

First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)

ftp_pasv($conn_id, true); // after ftp_login

Second, you have to change to dir first:

ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);

If you want to know what's really going on, try to get error message with error_get_last() or $php_errormsg

Upvotes: 1

Related Questions