使用CURL和PHP将文件FTP上传到远程服务器上传空白文件

时间:2012-03-28 11:29:10

标签: php curl

我正在尝试将文件上传到远程服务器,但看起来源文件什么都不做。我得到的只是服务器上的空白文件。 我的代码是这样的:

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

?>

1 个答案:

答案 0 :(得分:23)

在我的头撞击键盘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