php 使用 cURL 上传图片到远程服务器 [英] Php upload image to remote server with cURL

查看:37
本文介绍了php 使用 cURL 上传图片到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 php 中使用 cURL 将图像上传到远程图像服务器.我有这段代码,它在网络服务器上:

I would like to use cURL in php to upload an image to a remote image server. I have this piece of code, it's on the webserver:

<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中处理上传的图像文件的方式:

On the image server I've got an image upload handling php file, which worked very well on localhost, but I would like to use it on the remote server. This is how I handled the uploaded image file in the receiver.php:

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

我想直接把图片文件传给远程服务器脚本,这样就不用重写整个上传脚本了.我试图将图像名称、类型、大小作为发布变量发布,但我没有得到 ['tmp_name'],因为它不在本地主机上.

I want to directly pass the image file to the remote server script, so this way I don't need to rewrite the whole upload script. I tried to post the image name, type, size as post variables, but I haven't got the ['tmp_name'] since it's not on localhost.

我该如何解决这个问题?感谢大家的帮助!

How can I solve this? Thank you guys for any help!

推荐答案

这是一个可能的解决方案;

Here's a possible solution;

  • 在您的网络服务器上处理上传并将上传的文件移动到本地临时位置
  • 然后向远程服务器发出 curl POST 请求,并告诉它上传的文件名是什么 &数据是;作为 base64_encoded 字符串
  • 远程服务器脚本接收作为标准 http 帖子的上传
  • 它现在要做的就是解码文件数据,将其保存为指定的文件名

所以,这就是解决方案的样子:

So, this is how the solution looks like:

抱歉,我没有测试过这个,但它应该可以工作.

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!
";
    }
}

?>

<!-- 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 上,您可以执行以下操作:

Then, on the receiver.php, you can do the following:

// 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";
}

这篇关于php 使用 cURL 上传图片到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆