简体   繁体   English

我如何使用PHP上传文件

[英]how can i upload a file using PHP

On this page: http://developers.box.com/docs/ 在此页面上: http://developers.box.com/docs/http://developers.box.com/docs/

Upload a file using cURL: 使用cURL上传文件:

METHOD
POST /files/content
EXAMPLE REQUEST
curl https://api.box.com/2.0/files/content \
-H "Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN" \
-F filename1=@FILE_NAME1 \
-F filename2=@FILE_NAME2 \
-F folder_id=FOLDER_ID

But Now, I want to upload a file using php, how could I do it? 但是现在,我想用php上传文件,我怎么能这样做? my code: 我的代码:

<?php     
$params = array();
$params['folder_id'] = '485272014';

$u_file = fopen("D:\code\php\bcs\test.data", "r");

$params['filename1'] = $u_file;

$params = json_encode($params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/content");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

curl_setopt($ch, CURLOPT_UPLOAD, true);



curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key=API_KEY&auth_token=TOKEN"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

fclose($u_file);

?> 

it didn't work, and I run the script using: php -f test.php 它不起作用,我使用: php -f test.php运行脚本

  1. I don't think POST form data can accept file handler created using 我不认为POST表单数据可以接受使用创建的文件处理程序
    fopen("D:\\code\\php\\bcs\\test.data", "r");

    Try accessing file handler by using @ instead . 尝试使用@代替访问文件处理程序。 Btw, change \\ to / so you don't accidentally put some character as escape character : 顺便说一句,改变\\到/所以你不小心把一些角色作为转义字符:
    $u_file = "@D:/code/php/bcs/test.data";

  2. You shouldn't json_encode the content, what if your file content is not text (says, an image/binary file) . 您不应该对内容进行json_encode ,如果您的文件内容不是文本(例如,图像/二进制文件),该怎么办?

  3. I think this line gives you problem too. 我认为这条线也给你带来了问题。 Tried my code with this option, threw me a weird "441 Required length" error . 用这个选项试了我的代码,给我一个奇怪的“441必需长度”错误。 My code works fine without this option : 没有这个选项我的代码工作正常:
    curl_setopt($ch, CURLOPT_UPLOAD, true);

Finally, here is my working code : 最后,这是我的工作代码:

<?php
public function upload_file()
{
   $url = 'https://api.box.com/2.0/files/content';

   $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_ENCODING, "UTF-8");

   //this is my method to construct the Authorisation header
   $header_details = array($this->default_authen_header());
   curl_setopt($ch, CURLOPT_HTTPHEADER, $header_details);

   $post_vars = array();
   $post_vars['filename'] = "@C:/tmp_touch.txt";
   $post_vars['folder_id'] = 0;

   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
   curl_setopt($ch, CURLOPT_URL, $url);

   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}
?>
<?php
    // ENTER YOUR DEVELOPER TOKEN
    $token = "ekdfokeEdfdfkosdkoqwekof93kofsdfkosodSqd";

    $url = "https://upload.box.com/api/2.0/files/content";
    if (isset($_POST['btnUpload'])) {
        $file_upload = $_FILES['file']['tmp_name'];
        $json = json_encode(array(
                                'name' => $_FILES['file']['name'], 
                                'parent' => array('id' => 0)
                            ));
        $fields = array(
                      'attributes' => $json,
                      'file'=>new CurlFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name'])
                  );

        try {
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer '.$token, 
                'Content-Type:multipart/form-data'
            ));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $response = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $response = $e->getMessage();
        }

        print_r($response);
    }

?>

<form method="post" name="frmUpload" enctype="multipart/form-data">
    <tr>
        <td>Upload</td>
        <td align="center">:</td>
        <td><input name="file" type="file" id="file"/></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td align="center">&nbsp;</td>
        <td><input name="btnUpload" type="submit" value="Upload" /></td>
    </tr>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM