简体   繁体   English

使用cURL将文件发布到安全文件夹中(https://)

[英]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. 问题是使用cURL将txt文件上传到安全的文件夹( https://www.mydomain.com/myfolder/ )中。

I have a relevant ftp details to connect that folder. 我有相关的ftp详细信息来连接该文件夹。 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 上传文件时返回error_no:7

<?
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_setopt($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 根据此列表 ,错误代码7为

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. 您正在建立ftp连接,但要添加POST字段。 Also, nothing of this has to do with https. 另外,这与https无关。 What exactly are you trying to do? 您到底想做什么?

我不知道为什么您必须使用cURL,但是PHP具有自己的FTP功能 ,这会使生活变得更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM