dongyuan7981 2013-05-10 20:32
浏览 15

使用PHP上传文件

I have set up an HTML form to select a file and submit it to a PHP script which will upload it. I cannot use move_uploaded_files() because Box's API requires that I add a HTTP Header for Authorization: access_token. What I've done is set up my own POST method using the cURL library.

The problem I'm running into is setting the filename correctly, as it requires the full path of the file. I cannot get the full path of the file from the HTML form and using $_FILES['filename']['tmp_name'] uploads a .tmp file which I do not want. Does anyone know the solution to this problem? Thanks a lot!

My code:

public function upload_file($file) {
        $url = 'https://api.box.com/2.0/files/content';
        $params = [
            'filename' => '@'.$file['tmp_name'],
            'folder_id' => '0'
        ];
        $header = "Authorization: Bearer ".$this->access_token;
        $data = $this->post($url, $params, $header);
    }

public function post($url, $params, $header='') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        if(!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
        }
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
  • 写回答

2条回答 默认 最新

  • dongliao9018 2013-05-14 12:51
    关注

    Suggest you do one of the following:

    1. Use move_uploaded_files to upload file into some directory and use that location to send the file to box using curl. After successful upload you can delete the file from this directory.
    2. Instead of uploading the file through PHP, you can upload them client side using the cors http://developers.blog.box.com/2013/05/13/uploading-files-with-cors/

    Another question which I have in my mind is how are you keeping your access_token refreshed?

    • Vishal
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算