简体   繁体   English

如何通过curl将文件数据发送到$ _FILES中的另一台服务器

[英]How to send file data via curl to another server in $_FILES

I have two servers one is application server while another one is API server, API server reads data from $_FILES . 我有两个服务器,一个是应用程序服务器,另一个是API服务器,API服务器从$_FILES读取数据。

So my question is how can I send the File data to API Server so that it can get data in $_FILES ? 所以我的问题是如何将文件数据发送到API Server,以便它可以获取$_FILES数据?

I need CURL to do that, no form post. 我需要CURL来做,没有表单发布。

Thanks, 谢谢,

WA 西澳

Here is a simple script to send a file with php/cURL via POST: 这是一个简单的脚本,可通过POST发送带有php / cURL的文件:

<?php
$target_url = 'http://127.0.0.1/accept.php';
    //This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('./sample.jpeg');
    /*  the at sign '@' is required before the
     * file name.
     */
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result=curl_exec ($ch);
    curl_close ($ch);
    echo $result;

And here is the corresponding script to accept the file. 这是接受文件的相应脚本。

 <?php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
    if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }
?>

From php.net 来自php.net

<?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM