FTP upload file to distant server with CURL and PHP uploads a blank file

前端 未结 1 1814
情歌与酒
情歌与酒 2020-12-06 07:24

I\'m trying to upload a file to a distant server, but looks like the source file does nothing. All i get is a blank file on the server. My code is this:

<         


        
相关标签:
1条回答
  • 2020-12-06 08:16

    After 2 days of banging my head against the keyboard.. finally i did it.. Here is how:

    <?php
    
        if (isset($_POST['Submit'])) {
     if (!empty($_FILES['upload']['name'])) {
        $ch = curl_init();
        $localfile = $_FILES['upload']['tmp_name'];
        $fp = fopen($localfile, 'r');
        curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']);
        curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
        curl_setopt($ch, CURLOPT_UPLOAD, 1);
        curl_setopt($ch, CURLOPT_INFILE, $fp);
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
        curl_exec ($ch);
        $error_no = curl_errno($ch);
        curl_close ($ch);
            if ($error_no == 0) {
                $error = 'File uploaded succesfully.';
            } else {
                $error = 'File upload error.';
            }
     } else {
            $error = 'Please select a file.';
     }
    }
      echo $error;  
    ?> 
    

    Here is the source, from where i found the solution

    0 讨论(0)
提交回复
热议问题