【问题标题】:File uploaded to onedrive via php is empty通过php上传到onedrive的文件为空
【发布时间】:2019-08-17 23:49:38
【问题描述】:

我正在使用下面的代码将文本文件上传到 onedrive。文件成功上传到我的 onedrive 文件夹,但内容为空。

我将内容长度设置为 0

这里是文档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";

【问题讨论】:

  • 如果您将 Content-Length 设置为 0,为什么您会期望非空内容?

标签: php onedrive


【解决方案1】:

我找到了解决方案。该文件是空的,因为我实际上没有上传任何内容。所以我需要读取文件然后上传它

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

内容长度应设置为文件大小并且现在可以使用

"Content-Length:  $fileSize1"

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2011-12-24
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多