dongsibao8977 2019-06-12 06:38
浏览 126

将文件附加到客户端的json并在php中获取api服务器

I am trying to send file and some other data by cruel to API . i want to appended data to JSON file and send. I can do it but in server i can not get uploaded file. When i don't use format JSON i have not any problem that i access to $_file but in JSON I have not any file i have some data string only

client:

if (isset($_REQUEST['action'])) {
    if ($_REQUEST['action'] == 'submit') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        //curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

        $filePath = $_FILES['file_upl']['tmp_name'];
        $type = $_FILES['file_upl']['type'];
        $fileName = $_FILES['file_upl']['name'];
        echo "filePath::" . $filePath;
        echo "<br>fileName:" . $fileName;
         $data = (['name' => 'Foo', 'file_upl' => curl_file_create($filePath, $type, $fileName)] );

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $data ) );
        $result = curl_exec($ch);
        curl_close($ch);
        echo "<br>result: " . $result;
    }
}?>
<form name="file_up" action="" method="POST" enctype="multipart/form-data">
    Upload your file here
    <input type="file" name="file_upl" id="file_upl"/>
    <input type="submit" name="action" value="submit"/>
</form>

serve:

  $data = json_decode(file_get_contents('php://input'), true);
        echo "<pre>";
        echo 'in web SErvice: <br/>';
        echo (print_r($data));
        echo (print_r( $data->)); 
        echo $data->file_upl->name;
        echo $data->file_upl->tmp_name;

move_uploaded_file($data->file_upl->name, Yii::app()->basePath . "/files/" . $data->file_upl->postname);

result:

stdClass Object
(
    [name] => Foo
    [file_upl] => Array
        (
            [0] => stdClass Object
                (
                    [name] => C:\xampp\tmp\php8843.tmp
                    [mime] => image/jpeg
                    [postname] => 1-7.jpg

it is important for me that use JSON format. please tell me how can use std Class Object in to method move_uploaded_file that can be get file.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试
    • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
    • ¥15 教务系统账号被盗号如何追溯设备
    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 再不同版本的系统上,TCP传输速度不一致
    • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题