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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集