skip to Main Content

I have recently installed phpseclib and trying to upload a simple CSV file to a remote SFTP server but not getting any success.

The local file is in the root directory and the remote path is also going to the root.

Any suggestions would be great.


    use phpseclib3NetSFTP;

    $host = '';
    $user = '';
    $password = '';

    $sftp = new SFTP($host);

    if (!$sftp->login($user, $password)) {
        throw new Exception('Login failed');
    }

    echo 'Login successful';

    $file_to_upload = 'filename.csv';
    $remote_file_path = '/upload/filename.csv';

    $sftp->put($remote_file_path, $file_to_upload, SFTP::SOURCE_LOCAL_FILE);

2

Answers


  1. Chosen as BEST ANSWER

    I just noticed the actual file was not on the remote server to transfer.


  2. You say that "the local file is in the root directory" – the root of what? The server? The directory containing a particular project? Your user’s home directory? PHP is not going to assume any of these.

    Generally, it’s best to always use absolute paths for local files, as relative paths are not always relative to what you expect. So instead of ‘filename.csv’, you should refer to something like ‘/home/simon/my-project/uploads/filename.csv’

    You can use the magic constant __DIR__ to refer to the directory of whatever PHP script you write it in, then use ‘/../’ for "parent of", e.g. if the script is in /home/simon/my-project/public/test.php then you could write $localCsvFile = __DIR__ . '/../uploads/filename.csv';

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search