'Unable to open file stream for given path error with MSGraph SDK
I have a web app I wrote in JS. There is a form that allows file upload, which is then handled by PHP.
I have an account already, and I've registered an application under MS Azure and set the following permissions:
Files.Read
Files.ReadWrite
Files.ReadWrite.All
Sites.ReadWrite.All
User.Read
User.ReadWrite
Here is the code I use:
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
if(isset($_FILES)){
$end_result = new return_object();
$message = "stock msg";
$row_error_object_array = array();
$overall_error_array = array();
$general_message = "stock general msg.";
$success = true;
$temp_file_name = $_FILES['file']['tmp_name'];
$original_file_name = $_FILES['file']['name'];
$company_id = $_POST['company_id'];
// Find file extention
$ext = explode ('.', $original_file_name);
$ext = $ext [count ($ext) - 1];
$new_file_name = $original_file_name;
// Remove the extention from the original file name
$file_name = str_replace ($ext, '', $original_file_name);
// $new_name = 'videos/'.$file_name . $ext;
$path_prefix = "files/$company_id";
if(!is_dir($path_prefix)){
mkdir($path_prefix, 0750, true);
}
// $new_name = $path_prefix . "/" . $new_file_name . date("Y-m-d h:i:sa") . $ext;
$new_name = $path_prefix . "/" . $file_name . date("Y-m-d h-i-sa") . "." . $ext;
// $new_name = $new_file_name;
if (move_uploaded_file ($temp_file_name, $new_name)) {
// echo "we moved the file!<br>\n";
$general_message = $general_message . "File Move Success.";
$file_name = $_FILES["file"]["name"];
$file_type = $_FILES["file"]["type"];
$file_size = round($_FILES["file"]["size"] / 1024, 2) . " Kilo Bytes";
$general_message = $general_message . "Trying to upload.";
$guzzle = new \GuzzleHttp\Client();
$tenantId = 'my_tenant_id';
$clientId = 'my_client_id';
$clientSecret = 'my_client_secret';
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token';
$user_token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$user_accessToken = $user_token->access_token;
$general_message = $general_message . "Creating new Graph.";
$graph = new Graph();
$graph->setAccessToken($user_accessToken);
$graph->createRequest("PUT", "/me/drive/root/children/".$_FILES["file"]["name"]."/content")
->upload($_FILES["file"]["tmp_name"]);
// $graph->createRequest("PUT", "/me/drive/root:/".$_FILES["file"]["name"]."/content")
// ->upload($_FILES["file"]["tmp_name"]);
$general_message = $general_message . "File uploaded!
}
else {
// $error = $_FILES["file"]["error"];
// $response = array('success' => false, 'msg' => $error);
// echo json_encode($response);
$success = FALSE;
$message = "Fail - File wasn't moved.";
$general_message = "Fail - File wasn't moved.";
}
$end_result->success = $success;
$end_result->message = $message;
$end_result->row_error_object_array = array();
$end_result->overall_error_array = array();
$end_result->general_message = $general_message;
print_r(json_encode($end_result));
}
else{
$end_result = new end_result_upload_employee_info();
$end_result->success = true;
$end_result->message = "No file!";
$end_result->row_error_object_array = array();
$end_result->overall_error_array = array();
$end_result->general_message = "NO FILE!!!!";
print_r(json_encode($end_result));
}
Basically I'm moving the file to the server's file structure first, and then try to upload it in OneDrive. I plan to remove the part of saving to the server first when I get the OneDrive upload to work.
Right now I'm stonewalled and I'm not sure how to proceed. I also attempted this answer, but I'm getting an "interaction_required" error from Guzzle.
As much as possible, I want the upload to be fully automated, one where the user won't be required to log in, in order to make the user experience as seamless as possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|