0

I am trying to implement upload local file to server via sftp curl method but facing File upload error. Error number: 7.

Here is my code.

<?php

$ourFileName = "/var/www/html/example.com/100092923.csv";

$fpSend = fopen($ourFileName, 'r');

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, "sftp://example:@example/confirm/" . $ourFileName);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_PORT, 22);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, '/var/www/html/example.com/newcsv/ddd.ppk');
curl_setopt($ch, CURLOPT_SSH_PUBLIC_KEYFILE, '/var/www/html/exmple.com/newcsv/ddd.pub');
curl_setopt($ch, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PUBLICKEY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
$fp = fopen($ourFileName, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($ourFileName));
curl_exec($ch);


fclose($fpSend);

$error_no = curl_errno($ch);

if ($error_no == 0) {
    $error = 'File uploaded succesfully.';
} else {
    $error = 'File upload error. Error number: ' . $error_no;
}

if (curl_error($ch)) {
    $error_msg = curl_error($ch);

}
echo $error;
curl_close($ch);

?>

let me know if any one has a solution to our problem.

1 Answer 1

0

cURL Error (7): couldn't connect to host

This would suggest the connection is being blocked. Using a non standard port for ssh/sftp can do this.

Are you able to connect via sftp using a standard ftp client like filezilla? Rule out connection details issue before looking at your code.

A quick look over your code and everything looks fine.

1
  • thanks for your answer but if i use Filezilla then its working fine but if i try via curl then only . above issue arised. Jun 10, 2019 at 3:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.