php
  • curl
  • remote-server
  • 2016-03-03 155 views 2 likes 
    2

    我想在php中使用cURL將圖像上傳到遠程圖像服務器。我有這樣一段代碼,它是在Web服務器上:Php上傳圖像到遠程服務器與捲曲

    <form enctype="multipart/form-data" encoding="multipart/form-data" method="post" action="webform.php"> 
    <input name="somevar" type=hidden value='.$somevar.'> 
    <input name="uploadfile" type="file" value="choose"> 
    <input type="submit" value="Upload"> 
    </form> 
    

    和:

    if (isset($_FILES['uploadfile'])) { 
    
    $filename = $_FILES['uploadfile']['tmp_name']; 
    $handle = fopen($filename, "r"); 
    $data  = fread($handle, filesize($filename)); 
    $POST_DATA = array(
        'somevar' => $somevar, 
        'uploadfile' => $data 
    ); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, 'http://1.1.1.1/receiver.php'); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA); 
    $response = curl_exec($curl); 
    curl_close ($curl); 
    echo $response; 
    
    } 
    

    在我有一個圖片上傳處理PHP文件,它工作得很好localhost上圖像服務器,但我想在遠程服務器上使用它。這是我如何處理在receiver.php上傳的圖片文件:

    move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file) 
    

    我想直接通過圖像文件到遠程服務器腳本,所以這種方式,我不需要重寫整個上傳腳本。我試圖發佈圖像名稱,類型,大小作爲後變量,但我沒有得到['tmp_name'],因爲它不在本地主機上。

    我該如何解決這個問題?謝謝你們的幫助!

    回答

    3

    這是一個可能的解決方案;

    • 處理你的Web服務器上上傳和上傳的文件移動到本地臨時位置
    • 然後做出捲曲POST請求到遠程服務器,告訴它上傳的文件名&數據是什麼;爲Base64編碼字符串
    • 遠程服務器腳本接收上傳作爲一個標準的HTTP POST
    • 所有現在需要做的就是解碼的文件數據,將其保存爲指定的文件名

    那麼,這是怎麼了該解決方案看起來像:

    對不起,我沒有測試這個,但它應該工作。

    的index.php

    <?php 
    
    // Handle upload 
    if(isset($_POST["submit"])) 
    { 
        // Move uploaded file to a temp location 
        $uploadDir = '/var/www/uploads/'; 
        $uploadFile = $uploadDir . basename($_FILES['userfile']['name']); 
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) 
        { 
         // Prepare remote upload data 
         $uploadRequest = array(
          'fileName' => basename($uploadFile), 
          'fileData' => base64_encode(file_get_contents($uploadFile)) 
         ); 
    
         // Execute remote upload 
         $curl = curl_init(); 
         curl_setopt($curl, CURLOPT_URL, 'http://1.1.1.1/receiver.php'); 
         curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
         curl_setopt($curl, CURLOPT_POST, 1); 
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
         curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadRequest); 
         $response = curl_exec($curl); 
         curl_close($curl); 
         echo $response; 
    
         // Now delete local temp file 
         unlink($uploadFile); 
        } 
        else 
        { 
         echo "Possible file upload attack!\n"; 
        } 
    } 
    
    ?> 
    
    <!-- The data encoding type, enctype, MUST be specified as below --> 
    <form enctype="multipart/form-data" action="index.php" method="POST"> 
        <!-- MAX_FILE_SIZE must precede the file input field --> 
        <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> 
        <!-- Name of input element determines name in $_FILES array --> 
        Send this file: <input name="userfile" type="file" /> 
        <input type="submit" value="Send File" /> 
    </form> 
    

    然後,在receiver.php,你可以做到以下幾點:

    // Handle remote upload 
    if (isset($_POST['fileName']) && $_POST['fileData']) 
    { 
        // Save uploaded file 
        $uploadDir = '/path/to/save/'; 
        file_put_contents(
         $uploadDir. $_POST['fileName'], 
         base64_decode($_POST['fileData']) 
        ); 
    
        // Done 
        echo "Success"; 
    } 
    
    +0

    我會盡快嘗試,回信是怎麼回事。謝謝。 – nobodyknowshim

    +0

    謝謝,它的工作原理!我想指出未來的訪問者,如果你想在你的代碼中實現它,這個方法需要更多的安全性。您需要:檢查擴展名,MIME類型,大小,生成隨機名稱,move_uploaded_file,使用imagecreatefromjpg和imagejpeg php函數。在映像服務器上,您需要:再次執行上述所有任務,以確保如果有人直接向您的映像服務器發送請求,則無人能夠避免安全檢查。 – nobodyknowshim

    相關問題