使用cURL将图片上传到远程服务器的Php代码

6

我希望在php中使用cURL将图片上传到远程图片服务器。我有以下代码,它位于Web服务器上:

<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文件,在本地主机上运行得非常好,但我想在远程服务器上使用它。这是我在receiver.php中处理上传的图像文件的方式:
move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)

我希望能够直接将图像文件传递给远程服务器脚本,这样我就不需要重写整个上传脚本。我尝试以POST变量的形式提交图像名称、类型和大小,但由于它不在本地主机上,所以我没有得到['tmp_name']。
我该如何解决?谢谢大家的帮助!
1个回答

9
这里有一个可能的解决方案:
  • 在您的Web服务器上处理上传并将上传文件移动到本地临时位置
  • 然后向远程服务器发出curl 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";
}

我会尽快尝试并回复结果。谢谢。 - nobodyknowshim
1
谢谢,它有效!我想指出给未来的访问者,如果你想在你的代码中实现这种方法,需要更多的安全性。你需要:检查扩展名、mime类型、大小、生成随机名称、move_uploaded_file、使用imagecreatefromjpg和imagejpeg php函数。在图像服务器上,你需要再次执行所有上述任务,以确保如果有人直接向你的图像服务器发送请求,也无法避免安全检查。 - nobodyknowshim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接