简体   繁体   中英

PHP cURL SFTP Not Working

I am trying to upload a text file to an Amazon FTP site (for Amazon Product Ads).

I want to use PHP and cURL because I know I can use cURL for FTP-ing to another host we use, and I want to use the same code (so I don't want to use phpseclib). I'm pretty sure that cURL should be able to do this.

I have my connection details from Amazon (username, password and host address), which I know work 100% because I have used FileZilla to connect and that all works fine, and I'm using the same connection details in my cURL code.

Here is my code:

$ftp_username = 'xxxx';
$ftp_password = 'xxxx';
$ftp_url = 'dar-eu.amazon-digital-ftp.com';
$localfile = 'feeds/amazon-gifts.txt';
$remotefile = 'amazon-gifts.txt';

$ch = curl_init();
$fp = fopen($localfile,'r');

curl_setopt($ch, CURLOPT_URL, 'sftp://'.$ftp_username.':'.$ftp_password.'@'.$ftp_url.'/'.$remotefile);
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);

if (curl_errno($ch) == 0) {
    echo 'File uploaded succesfully.';
} else {
    echo 'File upload error. '.curl_errno($ch).' - '.curl_error($ch);
}

curl_close($ch);

I'm not specifying a port number here, because I don't with FileZilla either. However I have tried both 22 and 21, 22 works and I get the error below, and 21 says could not connect . So I'm pretty sure the above code connects to the server ok.

The error message I'm getting is:

9 - Upload failed: Permission denied (3/-31)

Like I said, I'm using the same connection details with FileZilla and cURL, and FileZilla just works, not errors, no permission denied messages.

I've contacted Amazon support and they said because there is no problem connecting using FileZilla then the problem must be with my code.

Can anyone help..? Am I missing an option that I should be setting..?

UPDATE

From the log:

* About to connect() to dar-eu.amazon-digital-ftp.com port 22 (#0)
*   Trying 46.137.126.98... * connected
* Connected to dar-eu.amazon-digital-ftp.com (46.137.126.98) port 22 (#0)
* SSH authentication methods available: publickey,password
* Using ssh public key file id_dsa.pub
* Using ssh private key file id_dsa
* SSH public key authentication failed: Unable to open public key file
* Initialized password authentication
* Authentication complete
* Upload failed: Permission denied (3/-31)
* Connection #0 to host dar-eu.amazon-digital-ftp.com left intact
* Closing connection #0

So it clearly completes authentication using the password.

Any ideas..?

The solution to this is to check your sub folders when connecting to a third party FTP site.

Using FileZilla, even though I didn't specify a default directory to go to (so I presumed the root), it automatically took me to a sub folder: /ebs_data/[my username]/ .

When using cURL I was trying to upload to / (root), where I didn't have permission.

FileZilla = /ebs_data/[my username]/ (where I do have permissions)

cURL = / (root - where I don't have permissions)

...because FileZilla jumped me directly to the sub folder, and I missed it.

What if you try with phpseclib, a pure PHP SFTP implementation? eg.

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('dar-eu.amazon-digital-ftp.com');
if (!$sftp->login('xxxx', 'xxxx')) {
    exit('bad login');
}

$sftp->put('amazon-gifts.txt', 'feeds/amazon-gifts.txt', NET_SFTP_LOCAL_FILE);

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