chinazaike
chinazaike

Reputation: 517

File uploaded to onedrive via php is empty

Am using this code below to upload a text file to onedrive. the file successfully gets uploaded to my onedrive folder but it's content is empty.

I set content length to 0

Here is the documentation link

//PUT /me/drive/items/{parent-id}:/{filename}:/content
//PUT /me/drive/root:/FolderA/FileB.txt:/content


$filesize1=filesize('test.txt');
$file1 = file_get_contents('test.txt');


$link = "https://graph.microsoft.com/v1.0/me/drive/items/Folderid:/test.txt:/content";
//$link = 'https://graph.microsoft.com/v1.0/me/drive/root:/folderid/test.txt:/content';

$curl=curl_init(); 
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_URL,$link);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'PUT');
curl_setopt($curl,CURLOPT_HEADER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
   //'Content-Type: application/json',   
'Content-Type: text/plain', 
//'Content-Type: image/png', 
"Content-Length: 0",                                                                           
    "Authorization: Bearer $access_token1")                                                                       
);  
//curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);

 $out = curl_exec($curl); 
var_dump($out);
 $codeCurl = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);

echo "<br><br>";

echo "success";

Upvotes: 0

Views: 276

Answers (1)

chinazaike
chinazaike

Reputation: 517

I found the solution. The file is empty because am actually uploading nothing. so I need to read the file and then upload it

$stream=fopen('test.txt', 'r')) {
$data = fread($stream, $fileSize1);
fclose($stream);

The content length should be set to filesize and is working now

"Content-Length:  $fileSize1"

Thanks

Upvotes: 1

Related Questions