0

我有两台服务器,一台是应用程序服务器,另一台是 API 服务器,API 服务器从$_FILES.

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

我需要 CURL 来做到这一点,没有表单发布。

谢谢,

西澳

4

2 回答 2

2

这是一个通过 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";
    }
?>
于 2013-06-25T06:52:03.943 回答
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);
?>
于 2013-06-03T05:41:23.200 回答