I'm trying to upload a file to Google Drive using API. Uploading process works fine but the problem is the uploaded file on the drive is empty(has nothing to show). Why?
Here is the uploading PHP code.
DEFINE("TESTFILE", $_FILES['myFile']);
DEFINE('TYPE', $_FILES["myFile"]["type"]);
$file = new Google_Service_Drive_DriveFile();
$file->setName('HelloWorld');
$file->setDescription('A test document');
$result = $service->files->create(
$file,
array(
'data' => TESTFILE,
'mimeType' => TYPE,
'uploadType' => 'media'
)
);
}
catch(Exception $e){
echo $e->getMessage();
}
And this is the HTML form:
<div>
<form enctype="multipart/form-data" method="POST">
<input type="file" name= "myFile">
<input type ="submit">
</form>
You are missing an intermediary step in your code.
You need to save the uploaded file to disk locally first ( as a temporary file ) and then read that file back into either a variable or file handle which you would then pass in place of TESTFILE in your $service->files->create() call.
Something like this ...
$file_tmp = $_FILES["myFile"]["tmp_name"];
$file_type = $_FILES["myFile"]["type"];
$file_name = basename($_FILES["myFile"]["name"];
move_uploaded_file($file_tmp, "path/to/upload/".$file_name);
$file_data = file_get_contents("path/to/upload/".$file_name);
$file = new Google_Service_Drive_DriveFile();
$file->setName('HelloWorld');
$file->setDescription('A test document');
$result = $service->files->create(
$file,
array(
'data' => $file_data,
'mimeType' => $file_type,
'uploadType' => 'media'
)
);
But please remember to validate your file uploads for proper type, size, etc. as best practice.
Related
I currently have a function that successfully uploads jpg files to google drive. I was wondering how to modify the function so that I can upload pdf, jpg, png, and docx files to google drive as well. Here is my code:
function uploadFiles($filePath) {
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents($filePath);
$createdFile = $this->service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
}
Any help is appreciated. Thanks.
In the case that the files of pdf, jpg, png, and docx are uploaded as pdf, jpg, png, and docx, when a file is uploaded with the method of Files: create in Drive API, it seems that the mimeType of the file is automatically detected. I thought that in your case, this situation can be used. So please modify your script as follows.
Modified script:
function uploadFiles($filePath) {
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid());
$file->setDescription('A test document');
$data = file_get_contents($filePath);
$createdFile = $this->service->files->create($file, array(
'data' => $data,
'uploadType' => 'multipart'
));
}
By above modification, for pdf, jpg, png, and docx, the uploaded file has the correct mimeType.
I want to upload files in a folder called "Files" to google drive.
It uploads without any problem, but I want to echo a string to my html file after i click my submit button. (I want to print generated file id's.) But it doesnt print anything in this code block.
Please help!
if (!empty($_POST)) {
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
echo "file uploaded"; // This is not working!
}
finfo_close($finfo);
header('location:'.$url);exit;
}
I can upload my file to google drive using php but I can't retrieve the link. Here's my php code for uploading the file to google drive :
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
}
finfo_close($finfo);
header('location:'.$url);exit;
I've done some research and according to google, this should be it:
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$upload = $service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type,
'fields' => 'embedLink'
)
);
}
finfo_close($finfo);
header('location:' . $upload->fields);exit;
As you can see I added 'fields' => 'embedLink' which requests for the embed link. fields has a lot of different functions but this one seems relevant to your question.
I'm not sure if $upload->fields is going to work or not because I've only tested it on google's emulator which doesn't show me any code. The link is supposed to be sent back in the response (in JSON format), so you might have to use json_decode() to get the data. You can also try $upload->embedLink.
I am trying to upload a photo that I have saved on my server onto my facebook via this facebook application.
My facebook request originally was:
$response = (new FacebookRequest(
$session,
'POST',
'/me/photos',
array(
'source' => '#' . $_FILES['file']['tmp_name'] ,
)
))->execute()->getGraphObject()->asArray();
/* handle the result */
print_r($response);
But apparently the syntax has changed now and I need to have something like this instead:
'source'=> new CURLFile(' '),
Not sure what to put as the parameters because my image is stored in a folder in my server.
My application allows the user to browse and submit a photo from their computer. This will be saved onto the server before it's posted onto facebook. I used the multiform/part:
<form method="post" enctype="multipart/form-data">
Please select a photo to upload <input type="file" name="file" id="file"><br><br>
<input type="submit" value="Upload" name="submit">
</form>
and the code I have to upload the photo to the server is as follows:
if(isset($_POST['submit'])) {
$file_type = $_FILES['file']['type']; //returns the file type
$allowed = array("image/jpeg", "image/gif", "image/png", "image/jpg"); //specifies allowed file types
if(!in_array($file_type, $allowed)) {
$error_message = 'Only jpeg, jpg, gif, and png files are allowed. <br>
Please click back and try again.';
echo $error_message;
exit();
}
$name = $_FILES['file']['name']; //original path of the uploaded file
$temp_name = $_FILES['file']['tmp_name']; //contains the path to the temporary file that resides on the server
if(isset($name)) {
if(!empty($name)) {
$location = 'uploads/'; //save photo to the folder: uploads
if(move_uploaded_file($temp_name, $location.$name)){
echo 'Photo was successfully uploaded.';
}
}
}
else {
echo 'Photo was unsuccessfully uploaded, click back and try again.';
}
I know the session works because I was able to POST a message onto facebook.
The CURLFILE method takes one main parameter, which is the path and filename. Modify your code as follows:
$response = (new FacebookRequest(
$session,
'POST',
'/me/photos',
array(
'source' => new CURLFile( $_FILES['file']['tmp_name'] )
)
))->execute()->getGraphObject()->asArray();
I have an upload form that has two file input's.
I want to rename the files so that each have a unique name. Here is what I have in my Controller
public function mainAction()
{
$upload = new Zend_File_Transfer();
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
$upload->addFilter('Rename', uniqid($file.'_').'.csv', $file);
}
$upload->receive();
}
Even though I have specified the file as the last paramter in setFilter, it renames both files at the same time so that they end up with the same name.
I figured out how to do it.
This is the form:
<input type="file" name="one">
<input type="file" name="two">
this goes in the controller
$renamefile1 = new Zend_Filter_File_Rename(array(
'target' => $path.'/file1.csv', // path to file
'overwrite' => true
));
//rename file 2 to file2
$renamefile2 = new Zend_Filter_File_Rename(array(
'target' => $path.'/file2.csv', // path to file
'overwrite' => true
));
$names = $upload->getfileName();
$file1 = $renamefile1->filter($names["one"]);
$file2 = $renamefile2->filter($names["two"]);