onedrive file upload using php – how to upload file in onedrive

onedrive file upload using php – how to upload file in onedrive

in this post we will show you onedrive file upload using php. For create onedrive file upload you need register you app and get CLIENT-ID, CLIENT-SECRET and CALLBACK-URL.

For add CLIENT-ID, CLIENT-SECRET and CALLBACK-URL go to src/functions.inc.php. Pass CALLBACK-URL as example/callback.php

onedrive file upload using php, we can upload files, images, videos or other type of documents. it will by using this code we can upload file or image at any folder or location of onedrive. if you have to create to upload file at root then do not pass any folder id.

HTTP request for onedrive file upload using php

PUT /drive/items/{parent-id}:/{filename}:/content
PUT /drive/root:/{parent-path}/{filename}:/content
PUT /drive/items/{parent-id}/children/{filename}/content

Example for onedrive file upload using php

PUT /drive/root:{item-path}:/content
Content-Type: text/plain

This is complete package for onedrive rest api using php ::

Download

<?php

// code for onedrive upload file using php
// go to downlode package and include header.inc.php 
require_once "header.inc.php";
// go to downlode package and in "src" folder and  include functions.inc.php 
require_once "../src/functions.inc.php";

// we Call this function to grab 
// a current false or access_token if none this is available.
$get_token = skydrive_tokenstore::acquire_token();


if (!$get_token) 
{ 
	// token not found 
	// If not found token at that time prompt to login. 
	// and Call skydrive_auth::build_oauth_url() to get the redirect URL and login procsee.
	echo "<div class=''>";
	echo "<img class='' src='statics/keyicon.png' width='35px' style='vertical-align: right;'>&nbsp";
	echo "<span class='' style='vertical-align: right;'><a href='".skydrive_auth::build_oauth_url()."'>Login with Onedrive</a></span>";
	echo "</div>";
	
} 
else 
{
	$real_uploadfile = $_FILES["uploadfile"]["name"];		
	$temp = explode(".", $_FILES["uploadfile"]["name"]);	
    $temp_ral = $temp[0];
	$newfilename = date("Ymd").round(microtime(true)) .$temp_ral. '.' . end($temp).$file_name;
	$file_tmp = $_FILES['uploadfile']['tmp_name'];
	// check folder is exist in over system 
	if (!file_exists('uploads')) {
		// create new folder
		mkdir('uploads', 0777, true);
	}
	$target_dir = "uploads/";	
	$uplode_path = $target_dir.$newfilename;
	move_uploaded_file($file_tmp,$uplode_path);
	$skydrive = new skydrive($token);
	// onedrive upload file using php
	$folderid = $_POST['folderid'];

?>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>	
<?php
	
	try {
		$response_data = $skydrive->put_file($_GET['folderid'], $uplode_path);
		// File was uploaded, return metadata.
		print_r($response_data);
	} 
	catch (Exception $exception) 
	{
		// An error accrue for onedrive file upload using php 
		echo "Error: ".$exception->getMessage();
		exit;
	}

}
require_once "footer.inc.php";
?>

You also like onedrive folder using php

4 thoughts on “onedrive file upload using php – how to upload file in onedrive”

  1. Hello, I have tried to download the example and it indicates that the repository or files no longer exist; Could you indicate or send me the sources? please ?

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  52  =  53

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US