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

时间:2012-10-10 12:33:44

标签: php file file-upload curl

我有两台服务器,一台是应用服务器,另一台是API服务器,API服务器从$_FILES读取数据。

所以我的问题是如何将文件数据发送到API服务器,以便它可以在$_FILES中获取数据?

我需要CURL才能做到这一点,没有任何形式的帖子。

谢谢,

WA

2 个答案:

答案 0 :(得分:1)

这是一个通过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;

这是接受该文件的相应脚本。

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

答案 1 :(得分:0)

来自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);
?>