php
  • curl
  • remote-server
  • 2016-03-03 132 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

    相关问题