Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i upload a file using PHP

Tags:

php

box-api

On this page: http://developers.box.com/docs/

Upload a file using 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? 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

like image 869
user1822862 Avatar asked Nov 14 '12 08:11

user1822862


People also ask

How do I upload a file to a form in PHP?

PHP - File Uploading. The selected file is sent to the temporary directory on the server. The PHP script that was specified as the form handler in the form's action attribute checks that the file has arrived and then copies the file into an intended directory. The PHP script confirms the success to the user.

What is the difference between upload and upload in PHP?

The index.php file holds code which is responsible for displaying the file upload form. On the other hand, the upload.php file is responsible for uploading a file to the server. Also, a file will be uploaded in the uploaded_files directory, so you need to make sure that this folder exists and is writable by the web-server user.

How to upload files to filestack using PHP?

1. The HTML Form 2. The PHP File Upload Script 1. Sign up for a Filestack Account 2. Start Uploading 1. The HTML Form First, we’ll create an HTML form that the user will see when they want to upload the file. Create a new folder for this example project, and within it, create an index.html file with the following code:

What do I need to learn PHP to upload files?

Prerequisites: A good grasp on HTML forms, and a working knowledge of PHP is helpful for understanding the guide, but not necessary to get a working product (simply use the last code snippit). There are many scripts available on the web to assist with uploading files to a server using a web browser.


2 Answers

  1. I don't think POST form data can accept file handler created using
    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) .

  3. I think this line gives you problem too. Tried my code with this option, threw me a weird "441 Required length" error . 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;
}
?>
like image 141
jingkee Avatar answered Oct 27 '22 00:10

jingkee


<?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>
like image 32
Josh LaMar Avatar answered Oct 27 '22 00:10

Josh LaMar