Uploading a file using the Google Drive API v3 and php via html form

I'm working on a program that would allow users to select a file using a HTML form, and then the program would upload said file to Google Drive and share it with a certain other user. I've been able to create a file in Google Drive and then share it; but once I tried uploading a file it all went downhill.
It's odd, because I think the code to upload the file is correct, so the issue must lie in how i get the file from the HTML form to the PHP file. My suspicion has sort of been confirmed by the fact that I attempt to echo $_FILES['myfile']['name'] nothing happens.
I'll upload two code snippets below, but I'll omit all the Google Drive api authentication for brevity's sake because that works.
index.php
<form action="../Next.php" method="POST" enctype="multipart/form-data">
<section class="file">
<span>Please select the file to be uploaded </span>
<br>
<label for="myfile">Select a file:</label>
<input type="file" name="myfile" required >
</section>
<section class="submission">
<input type="submit" value="Submit">
</section>
</form>
Next.php
$myfile=$_FILES['myfile']['tmp_name'];
$type=mime_content_type($myfile);
$drive = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('Test');
$file->setDescription('Test');
$file->setMimeType($type);
$data = file_get_contents($myfile);
$uploadedFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $type,
));
Please remember I have omitted the authentication code because I know that works.
I can't understand why this does nothing, from what I've been able to gather from looking around the internet the code is correct. I think the issue might have to do with the $_FILES['myfile'] array, and yet from all the documentation online it seems to be correct as well.
Answer
Solution:
if still not resolved then...
As you mentioned $_FILES is empty then i suggest to look at the file location in your project.
suppose yourindex.php
is inPROJECT->HOME->index.php
directory then yourNext.php
should be inPROJECT->Next.php
directory.
Also you have to give submit aname=submit
.
inNext.php
test either form is set or not
if(isset($_POST['submit'])){
print_r($_FILES['myfile']);
}
And if file is not being uploaded let me know the issue.
Answer
Solution:
Try adding multipart
$uploadedFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $type,
'uploadType' => 'multipart'
));
Answer
Solution:
Inital Set-up
Due to your HTML file specification I assume your project structure is the following:
project/
в”њв”Ђв”Ђ templates/
в”‚ в””в”Ђв”Ђ index.html
в””в”Ђв”Ђ Next.php
So I will start a php simple http server in the project folder using the command:php -S localhost:8000
Approach
In order for this to work you will have to adapt yourNext.php
code a little bit. I recommend using the last Drive APi version (v3). There is no methodinsert()
in v3 but it hascreate()
.
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$myfile=$_FILES['myfile']['tmp_name'];
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => "Test",
));
try{
$newFile = $service->files->create(
$fileMetadata,
array(
'data' => file_get_contents($myfile),
'mimeType' => mime_content_type($myfile),
'uploadType' => 'multipart'
)
);
} catch(Exception $e){
echo 'An error ocurred : ' . $e->getMessage();
}
Reference
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: please make sure the php redis extension is installed and enabled.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.