2
votes

I can upload .docx file to google drive from my apps, but when i tried to upload .doc file, this error appeared :

An error occurred: Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&key=AIzaSyCm1WqPp05lBRjKSpxdtHjS8lz6WLeoWlU: (400) Invalid mime type provided

That error appeared too when I uploaded .ppt .xls file. The documentation says we can store any MIME type in Google Drive. What's wrong here? Anybody knows?

This is my upload function :

function insertFile($title, $description, $parentId, $mimeType, $filename) {

$file = new DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);

if ($parentId != null) {
  $parent = new ParentReference();
  $parent->setId($parentId);
  $file->setParents(array($parent));
}

try {
  $data = file_get_contents($filename);

  $createdFile = $this->service->files->insert($file, array(
    'data' => $data,
    'mimeType' => $mimeType,
  ));

  return $createdFile;
} catch (Exception $e) {
  print "An error occurred: " . $e->getMessage();
  }    
}

[UPDATE] I'm using CI and this is my function in controller that calls insertFile function :

function upload() {
$title = $this->input->post('title');
        $description = $this->input->post('description');
        $parentId = $this->input->post('parentId');

        if ($_FILES["file"]["error"] > 0) {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        } else {
            $driveHandler = new DriveHandler($_SESSION['credentials']);
            $driveHandler->BuildService($_SESSION['credentials']);
            $ext = substr(strrchr($_FILES["file"]["name"],'.'),1);        
            if($title === ""){
                $fileTitle = $_FILES["file"]["name"];                
            } else {
                $fileTitle = "$title.$ext";
            }
            $driveHandler->insertFile($fileTitle, $description, $parentId, $_FILES["file"]["type"], $_FILES["file"]["tmp_name"]);              
}
2
What does $mimeType equal?doublesharp
$mimeType = $_FILES["file"]["type"]. In my view, I have this : form_upload('file');Irien
No, the actual value when it fails - the error is complaining about the MIME type.doublesharp
Google Drive should accept any mime type, my guess is that the error is actually in $this->service->files->insert()doublesharp
@doublesharp It turns out that the error is in my $mimeType. There is an additional double quote in $mimetype for .doc file. Thanks for replying my questionIrien

2 Answers

0
votes

When I try to use var_dump($mimeType) for .docx file, the result is :

string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

and for .doc file :

string(20) ""application/msword""

Based on the results, I found the problem. There is an additional double quote in $mimeType for .doc file. So I try to change my code like this :

  $data = file_get_contents($filename);
  $mime = str_replace('"', "", $mimeType);

  $createdFile = $this->service->files->insert($file, array(
    'data' => $data,
    'mimeType' => $mime,
  ));

It now works.

0
votes

Rather than just replacing double quotes, you can use a negative character class in regular expression with preg_replace() to replace any non alphanumeric, /, or . characters.

$mimeType = preg_replace("~[^a-zA-Z0-9/\.]*~", "", $mimeType);