Chirag B
Chirag B

Reputation: 2126

how to upload file using box.net API?

Form last 2 days i am trying to fix a issue of uploading a file in Box.net using php.

I have gone through Box.net API Documentation and copied the sample code they have given, the code that they have given- uploads file perfectly, But it redirects to that page where server response is printed(XML response).

I do not want to show user such screen, instead i would like to read those data and based on that response, display message of mine.

I have edited my code as per the guidance given by @GBD, now i am facing another issue.

The file I select does not upload , Instead it uploads some .tmp file. And when i hard code the file path in $_POST['new_file1'] it successfully uploads correct file. the file path i get is some thing like this C:\wamp\tmp\php1A.tmp not actual file path, when i echo $_FILES['new_file1']['tmp_name'].

Can any one tell me how can i get the file path from $_FILES ? So that i can pass it to $post

Following is my code:

<?php
$upload_url = 'https://upload.box.net/api/1.0/upload/i9g1fmnnddfdr4739sxvbpXXXXXXXX/480416060';
$tmpfile = $_FILES['new_file1']['tmp_name'];
$filename = basename($_FILES['new_file1']['name']);
$_POST['new_file1'] = '@'.$tmpfile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); 
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
<form action=""
  enctype="multipart/form-data" accept-charset="utf-8" method="POST">
<input type="file" name="new_file1" />
<input type="text" name="share" value="1" />
<input type="submit" name="upload_files" value="Upload File" />
</form>

Please help me out.

Thanks in advance.

Upvotes: 3

Views: 1750

Answers (1)

GBD
GBD

Reputation: 15981

Try

<?php

$tmpfile = $_FILES['new_file1']['tmp_name'];
$filename = basename($_FILES['new_file1']['name']);

$_POST['new_file1'] = '@'.$tmpfile;

$upload_url = 'https://upload.box.net/api/1.0/upload/i9g1fmnnddfdr4739sxvbpXXXXXXXX/480416060';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); 
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

Upvotes: 1

Related Questions