File posting using cURL into a secured folder(https://)
The problem is to upload a txt file into a secured folder(https://www.mydomain.com/myfolder/
) using cURL.
I have a relevant ftp details to connect that folder. here is my code, but it does not getting connected properly...
can any one please advise what mistake i did on this code. which returns error_no:7 while uploading file
<?
if (isset($_POST['Submit'])) {
if ($_FILES['upload']['name']!="")
{
$localfile = $_FILES['upload']['tmp_name'];
$newfile = $_FILES['upload']['name'];
$ch = curl_init();
$url = 'ftp://ftp_login:password@ftp.mydomain.com/myfolder/'.$newfile;
$fp = fopen ($localfile, "r");
curl_setopt($ch, CURLOPT_URL, $url);
curl_set开发者_JAVA百科opt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_FTPASCII, 1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $newfile);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
echo curl_error($ch);
echo $error_no = curl_errno($ch);
curl_close($ch);
//echo $result;
if ($error_no == 0)
{
$error = 'File uploaded succesfully.';
}
else
{
$error = 'File upload error.';
}
}
else
{
$error = 'Please select a file.';
}
}
?>
According to this list, error code 7 is
CURLE_COULDNT_CONNECT (7)
Failed to connect() to host or proxy.
Are you sure the server is reachable? Can you try manually?
Also, I'm not really getting what you are doing here. You are establishing a ftp
connection but adding POST fields. Also, nothing of this has to do with https. What exactly are you trying to do?
I don't know why you have to use cURL but PHP has its own FTP functions that will make life a bit easier.
精彩评论