Upload file to GDrive in php without user intervention - google-drive-api

I am using google-drive-sdk to upload a pdf file (dynamically generated in the php web app) to Gdrive.
About my app
On click of submit button the users see a THANKYOU displayed on the page. At the background
pdf file gets generated
and this file needs to be uploaded to Gdrive account --(configured the client id & secret key for the app in Google Console)
:
I am able to upload to the configured GDrive but first time to authenticate I run the url(redirect url given in Google Console) in browser(*say
http://localhost:3422/wordpress/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/pdf_results_upload.php
*)
This takes me to GDrve page and prompts Allow\Deny Access.
I click Allow and then am able to upload to Grdive.
But I would like this to happen without prompts for Allow\Deny for the user to interact.
Please give me details on what I need to add so that when user clicks the Submit button , automatically the generated file gets inserted to GDrive.
I read lot in net but couldn't understand and get it to work.
Please help me with steps and code to include.
The code I am using
<?php
include_once "templates/base.php";
if(!session_id()){session_start();}
require_once realpath(dirname(__FILE__) . '/../autoload.php');
$pdf_filename='';
try
{
if(isset($_SESSION['pdf_filename']) && $_SESSION['pdf_filename'])
$pdf_filename=$_SESSION['pdf_filename'];
}
catch(Exception $ex)
{
echo ("This is for GDrive Upload from CalWeb");
}
$pfdStoragePath=dirname(__FILE__)."/../../vendor/pdf/".$pdf_filename;
/************************************************
ATTENTION: Fill in these values! Make sure
the redirect URI is to this page, e.g:
http://localhost:8080/fileupload.php
************************************************/
$client_id = 'xxxx.apps.googleusercontent.com';
$client_secret = 'xxxxSB0tNVy';
$redirect_uri = 'http://localhost:3422/wordpress/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/roi_results_upload.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType("offline");
$client->setApprovalPrompt('force');
$service = new Google_Service_Drive($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
If we're signed in then lets try to upload our
file.
************************************************/
try {
if ($client->getAccessToken()) {
if($pdf_filename!=="")
{
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($pdf_filename);
$file->setDescription('Document');
$file->setMimeType('application/pdf');
$result = $service->files->insert(
$file,
array(
'data' => file_get_contents($pfdStoragePath),
'mimeType' => 'application/pdf',
'uploadType' => 'multipart'
)
);
//echo $result->webContentLink;
$pdf_filename="";
unset($_SESSION['pdf_filename']);
}
//if (isset($result) && $result) $_SESSION['webContentLink']=$result->webContentLink;
//echo $result->webContentLink;
}//if ($client->getAccessToken()) {
} catch (Exception $ex) {
echo '';
}
?>
<div class="box">
<div class="request">
<?php if (isset($authUrl)): ?>
<a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
<?php else: ?> <?php echo "Access Allowed" ?>
<?php endif; ?>
</div>
<?php if (isset($result) && $result): ?>
<div class="shortened">
<?php echo "aaa"; ?>
<?php echo $result->webContentLink; ?>
<?php echo "bbb"; ?>
</div>
<?php endif ?>
<?php if($pdf_filename==="") ?>
<?php echo "This is for GDrive Upload from MiniROI" ?>
</div>

Related

the site can't be reached while doing file uploading

I am new and I don't know where is the mistake, I think I have taken care the routes and all but still I am not sure. Please help me out.
So when I do localhost:8000/uploadfile/ it gives me this error
This site can’t be reached localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
And when I go through the detail URL it gives me:
the view[uploadfile] not found
This is my form:
<!DOCTYPE HTML>
<html>
<body>
<?php
echo Form::open(array('url'=>'/uploadfile','files'->'true'));
echo 'Select the file to upload';
echo Form::file('image');
echo Form::submit('upload file');
echo Form::close();
?>
</body>
</html>
My controller is:
<?php
namespace App\Http\Controllers;
use App\files;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
class UploadFileController extends Controller {
public function index() {
return view('uploadfile');
}
public function showUploadFile(Request $request) {
$file = $request->file('image');
//Display File Name
echo 'File Name: '.$file->getClientOriginalName();
echo '<br>';
//Display File Extension
echo 'File Extension: '.$file->getClientOriginalExtension();
echo '<br>';
//Display File Real Path
echo 'File Real Path: '.$file->getRealPath();
echo '<br>';
//Display File Size
echo 'File Size: '.$file->getSize();
echo '<br>';
//Display File Mime Type
echo 'File Mime Type: '.$file->getMimeType();
//Move Uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
}
Finally my route:
Route::get('/uploadfile','UploadFileController#index');
Route::post('/uploadfile','UploadFileController#showUploadFile');
You should try this:
your view file like:
Path:
yourproject folder/resources/views/uploadfile.blade.php
<form class="form-bordered" action="{{url('/uploadfile')}}" method="POST">
<div>
<lable>Select the file to upload</lable>
<input type="file" name="image">
<input type="submit" value="upload file">
</div>
</form>

Display latest wordpress featured images on static html page

I have a wordpress blog page installed in subfolder of my website(converted from html)and a static html home page.
I would like to display 3 latest posts and its featured images on home page. With code below i can display latest posts text but i dont know how to show featured images of posts. Into index.php of a wordpress custom theme i placed featured photo inside a div:
<div id="blogphoto"><?php the_post_thumbnail(); ?></div>
This is the code on static html index.php page which is pulling out latest post. Can anyone help me to get featured images of these posts too?
<div id="wp-post">
<?php
$args = array('numberposts'=>1);
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<h3>".$recent_post['post_title']."</h3> <br>";
echo "<span>".$recent_post['post_date']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post2">
<?php
$args = array('numberposts'=>1 , 'offset'=>1 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post3">
<?php
$args = array('numberposts'=>1 , 'offset'=>2 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
Please try to this code the_post_thumbnail getting the feature image
<?php
if ( $query->have_posts() ) {
$i = 1;
while($query->have_posts()) {
echo '<div id="wp-post-'.$i.'">';
$query->the_post();
?><h2><?php the_title(); ?></h2>
<?php
if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
echo '<p>post says it has a featured image</p>'; // double checking
the_post_thumbnail('thumbnail');
} else {
echo '<p>this post does not have a featured image</p>';
}
echo '</div>';
$i++;
}
} else {
echo '<p>no posts found</p>';
}
?>

Yii2: How to display Multiple images in view from path saved in database?

I have column called images in database which contain image paths.
Query:
$query = new Query;
$todo = (new yii\db\Query())
->select(['images'])
->from('room_types')
->andWhere("id = '$model->id'")
->all();
View :
<?php
foreach ($todo as $row)
{
?>
<?php echo Yii::getAlias('#web').'/'.$row; ?>
<?php
}
?>
Images path saved in db:
uploads/room_img/30.jpg;uploads/room_img/300.jpg;uploads/room_img/11928_569674493052762_732198968_n.jpg;
Tried with explode():
<?php
function room_images() {
$query = mysql_query("SELECT images FROM room_types WHERE id = $model->id");
while($row = mysql_fetch_array($query)) {
$e[] = explode(" ", $row[0]);
foreach($e as $r) {
echo $r;
}
}
}
?>
But nothing is showing
Use img method of Html class - reference
In your view file
use yii\helpers\Html;
// ...
<?php foreach ($todo as $key=>$row): ?>
<!-- html code if you need -->
<?php
foreach (explode(';', $row['images']) as $key_img => $value_img)
{
echo Html::img(Yii::getAlias('#web').'/'.$value_img);
}
?>
<!-- html code if you need -->
<?php endforeach; ?>
// ...

Wordpress Logout Link

How could I add a logout link in the else portion of code, after the "echo $upme->display();" ...
<?php
global $upme;
if (!is_user_logged_in()) {
echo $upme->show_registration();
echo $upme->login();
}
else { echo $upme->display();
}
?>
I tried a few things including the below code but I keep getting internal error ...
<?php
global $upme;
$html1 = 'Logout';
if (!is_user_logged_in()) {
echo $upme->show_registration();
echo $upme->login();
}
else { echo $upme->display();
echo $html1;
}
?>
Thank You
There's a small syntax error:
$html1 = 'Logout';
500 is generally an "I can't find that" error. What page is this from? get_permalink may be returning false. Try outputting that function to see what you get back.
<?php echo get_permalink(); ?>

How to get getRequestUri() in layout.phtml

In my zend layout.phtml I am serving my navigation like this:
<?php
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
echo '<li >home</li>';
echo '<li >Addbooks</li>';
echo '<li class="selected">EditBook</li>';
echo '<li>Adduser</li>';
echo '<li>Logout</li>';
}
?>
in a normal page I can get url this way
$this->view->assign('url',$this->getRequest()->getRequestUri());
$url = $this->getRequest()->getRequestUri();
$b=basename($url);
$this->view->assign('b',$b);
I want this url in my layout.phtm how can I pass this from bootstrap.php to layout.phtml? here my requirement is to add a class to li like this:
<li <?php if($b==EditBook) echo 'class="selected' ?> ">EditBook</li>
Better way to obtain it, is to write a view helper that will assign necessary variables
class Helper_Params extends Zend_Controller_Action_Helper_Abstract {
$view = $this->getActionController()->view;
$request = $this->getRequest();
$view->requestUri = $request->getRequestUri();
}
and init it in your Bootstrap
public function _initHelpers() {
Zend_Controller_Action_HelperBroker::addHelper ( new Helper_Params () );
}