2012-03-28 102 views
4

我想上傳文件到遠端服務器,但看起來像源文件什麼都不做。我所得到的只是服務器上的空白文件。 我的代碼是這樣的:FTP上傳文件到遠程服務器與CURL和PHP上傳空白文件

<?php 

    $c = curl_init(); 
    $file = "\PATHTOFILE\file.txt"; 
    $fp = fopen($file, "r"); 

    curl_setopt($c, CURLOPT_URL, "SERVERPATH/file.txt"); 
    curl_setopt($c, CURLOPT_USERPWD, "USER:PASSWORD"); 
    curl_setopt($c, CURLOPT_UPLOAD, 1); 
    curl_setopt($c, CURLOPT_INFILE, $fp); 
    curl_setopt($c, CURLOPT_INFILESIZE, filesize($file)); 

    curl_exec($c); 
     echo "Success"; 
    curl_close($c); 
    fclose($fp); 

?> 
+0

嘗試'echo curl_errno($ ch);'看看是否有任何錯誤 – safarov 2012-03-28 11:36:11

+0

嘗試它並得到0,現在它是什麼意思? – Dudipoli 2012-03-28 12:42:59

+0

SERVERPATH的格式是什麼?即只改變主機名,但留下所有其他的東西,標點符號等。 – dldnh 2012-03-28 19:43:15

回答

17

2天后敲我的頭靠在鍵盤的。最後我做到了.. 方法如下:

<?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; 
?> 

這裏是source,從那裏我發現解決方案

+0

嗨,我給+1,因爲CURLOPT_UPLOAD = 1沒有這個curlopt它真的上傳服務器上的空文件,我試圖上傳到下一個雲端 – Jimmmy 2017-03-31 09:40:51