2

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.

8
  • Check the remote directory have right permission to upload the file? Commented Apr 23, 2013 at 12:54
  • The remote directory has permissions of 777 for testing purposes, and I refreshed the directory and tried again with the same error, so I don't think that is the issue Commented Apr 23, 2013 at 12:57
  • remain things all goes in a right way. Most probably the user dont have the right privileges to access the remote server.. Commented Apr 23, 2013 at 13:02
  • Make sure you enable warnings in PHP. ftp_put will definitely issue a warning with more details on the error. Commented Apr 23, 2013 at 13:31
  • 1
    try to set passive mode with ftp_pasv($conn_id, true); Commented Apr 23, 2013 at 13:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

I looked at the servers error logs and I am getting this: [STDERR] PHP Warning: ftp_put(C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv) [<a href='function.ftp-put'>function.ftp-put</a>]: failed to open stream: No such file or directory in /chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php on line 19 I think I am destined not to get this to work
try to get the local file with $file = realpath(__DIR__).DIRECTORY_SEPARATOR.'checkslastmonth.csv'; Else the error it pretty clear: does the local file really exists at that location?
@Jim so it looks like you're running the script on a server and want to "upload" a file from you're local machine? Could it be that you need an html upload script and not a ftp upload?
Here is the result: /checkslastmonth.csv
Meaning I need to use a form? I am going to be uploading the same file, just updated, every time I use a particular program. That is why I went with the ftp script.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.