2014-09-05 50 views
0

我正在測試使用cURL上傳文件的可能性。 服務器的版本是PHP 5.4使用PHP CURL上傳文件 - 文件丟失

要從「發送」服務器我使用這個代碼上傳:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> <input type="file" name="Filedata" id="Filedata" /> 
    <br /> 
    <input type="submit" name="submit" value="Submit" /> 
</form> 

<?php 

if ($_POST['submit']) { 


    $real_name = $_FILES['Filedata']['name']; 

    $ch = curl_init(); 

    $data = array('name' => 'test.txt', 'file' => '@'.$real_name); 

    curl_setopt($ch, CURLOPT_URL, 'http://www.site1.com/upload_remote.php'); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

    curl_exec($ch); 
    curl_close($ch); 

我使用的print_r($數據)見$ data數組和一切似乎沒問題。

在「遠程」服務器(地址:http://www.site1.com/upload_remote.php)我有這樣的代碼:

$file = 'test2.txt' 

    if(move_uploaded_file($_FILES['file']['tmp_name'], $file)) { 

     echo "The file ". basename($_FILES['file']['name']). " has been uploaded"; 

    } 

但文件不存在 有一個在error_logs文件中沒有錯誤。 我忘了什麼嗎? 謝謝。

+1

在這裏看到:http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/ – 2014-09-05 18:58:45

+0

我tryed代碼但不成功 – 2014-09-05 19:20:07

+0

我的錯誤..工作正常!謝謝。 – 2014-09-05 19:42:36

回答

0

move_uploaded_file的第二個參數是目標文件應該移動的路徑,而您只是傳遞不是目錄的$文件。

Docs

+0

我已將目標更改爲:$ file ='/'; (相同的目錄),即使該文件不存在... – 2014-09-05 19:05:52

+0

您是否收到任何類型的錯誤?你可以添加else語句來檢查move_uploaded_file是否返回true或false。 – 2014-09-05 19:09:30

+0

返回false並且我的error_log文件也爲空 – 2014-09-05 19:16:59