1

I'm using a File Type input to pass FormData ($_FILES['files']) to PHP via Ajax

My PHP cURL looks like this:

$headers = array(
    "Authorization: Bearer " . <token>, 
    "Host: graph.microsoft.com",
    "Content-Type: multipart/form-data",
    "Content-Length: 0",
);

$filename = $_FILES['file']['name'];

$postfile = curl_init('https://graph.microsoft.com/v1.0/users/' . <userid> . '/drive/root:/<folder>/' . $filename . ':/content'); 
curl_setopt($postfile, CURLOPT_PUT, 1);
curl_setopt($postfile,CURLOPT_HEADER,false);
curl_setopt($postfile, CURLOPT_HTTPHEADER, $headers);
curl_setopt($postfile,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($postfile,CURLOPT_SSL_VERIFYHOST,0);

$result = curl_exec($postfile);
curl_close($postfile); 

This creates a file in the correct folder of the user's OneDrive with the correct extension and everything, but they are all empty (0 bytes).

I've tested this with many files of different types for hours, and it's always the same.

Where am I going wrong?

Sofistikat
  • 111
  • 1
  • 10
  • So you are trying to upload an existing file rather than create a new file? – Allen Wu Dec 02 '20 at 08:21
  • @AllenWu yes, that's right. I'm trying to upload existing files from my computer to OneDrive. – Sofistikat Dec 02 '20 at 08:29
  • But I didn't see where you get the file from your local machine. Use `file_get_contents` to get the file and pass it when you call the Microsoft Graph API. See https://stackoverflow.com/questions/47262443/upload-file-to-office-365-onedrive-business-account-using-php-curl?answertab=votes#tab-top and https://stackoverflow.com/questions/47708226/how-upload-large-files-to-onedrive-using-php-curl?answertab=votes#tab-top. – Allen Wu Dec 02 '20 at 08:55
  • The second answer is for uploading large file (>4M) to O365. But it should be the same for getting file using `file_get_contents`. – Allen Wu Dec 02 '20 at 08:57
  • @AllenWu shouldn't that be available from $_FILES['file']['tmp_name']? – Sofistikat Dec 02 '20 at 10:18
  • Didn't try `$_FILES['file']['tmp_name']`. Did you try with `file_get_contents`? – Allen Wu Dec 03 '20 at 01:30
  • @AllenWu I finally got it!!! After trying for days now! Is 2pm too early for a drink? hahaha – Sofistikat Dec 03 '20 at 03:19
  • Glad to know that. Cheers! – Allen Wu Dec 03 '20 at 03:24

2 Answers2

0

You have mising filedata Try add following to your curl

$fh_res = fopen($_FILES['file']['tmp_name'], 'r');

curl_setopt($postfile, CURLOPT_INFILE, $fh_res);
curl_setopt($postfile, CURLOPT_INFILESIZE, filesize($_FILES['file']['tmp_name']));
LuRy
  • 74
  • 5
0

I finally solved it with the following:

    $filename = $_FILES['file']['name'];
    $filecon = fopen($_FILES['file']['tmp_name'],'r');
    $filesize = filesize($_FILES['file']['tmp_name']);

    $headers = array(
        "Authorization: Bearer " . <token>, 
        "Host: graph.microsoft.com",
        "Content-Type: application/json",
        "Content-Length: " . $filesize,
    );

    $postfile = curl_init('https://graph.microsoft.com/v1.0/users/' . <userid> . '/drive/root:/<folder>/' . $filename . ':/content'); 
    
    curl_setopt($postfile, CURLOPT_PUT, 1);
    curl_setopt($postfile,CURLOPT_HEADER,0);
    curl_setopt($postfile, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($postfile, CURLOPT_UPLOAD, 1);
    curl_setopt($postfile, CURLOPT_INFILE, $filecon);
    curl_setopt($postfile, CURLOPT_INFILESIZE, $filesize);

    $result = curl_exec($postfile);
    curl_close($postfile); 

Hope this helps anyone looking for PHP cURL upload to OneDrive!

Sofistikat
  • 111
  • 1
  • 10