Share via

SharePoint Graph API Upload file

Benny Argo 0 Reputation points
2024-03-07T19:03:55.97+00:00

I am able to upload files of the following type but I can only view one of the following file types after it is successfully uploaded.

  • *.pdf - Works as expected. I am also able to replace the file.
  • *.txt - File is uploaded but there is a lot of unwanted text added to the file. ----------------------------190622897699579092183602 Content-Disposition: form-data; name=""; filename="Test118.txt" Content-Type: text/plain Test Text File 118 ----------------------------190622897699579092183602--
  • *.docx File is uploaded on the surface everything looks good. If I attempt to open the file from sharepoint I get the follow " Sorry. this file can't be opened for editing" and it will not open in Desktop App.
  • *.xlxs - Same as the *.docx file.

I am using PHP to upload the files. Code snippet below.

$URL_String = "https://graph.microsoft.com/v1.0/sites/xxxx/drives/yyyyyy/root:/".$this->decode($folderID)."/".$filename.":/content";
$data = array('file' => new CURLFILE($filepath . "/" . $filename), 'filename' => $filename);

$ch = curl_init();

curl_setopt_array(

$ch, array(

		CURLOPT_URL => $URL_String,

		CURLOPT_RETURNTRANSFER => true,

		CURLOPT_POSTFIELDS => $data,

		CURLOPT_ENCODING => '',

		CURLOPT_MAXREDIRS => 10,

		CURLOPT_SSL_VERIFYPEER => false,

		CURLOPT_TIMEOUT => 0,

		CURLOPT_FOLLOWLOCATION => true,

		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

		CURLOPT_CUSTOMREQUEST => 'PUT',

		CURLOPT_HTTPHEADER => array($_SESSION['access_token']),

		)

	);
$response = curl_exec($ch);



Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft Security | Microsoft Graph

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.