I am creating a web application to register students with codeigniter and I want to make the photos that are uploaded to the website stored in Google Drive and at the same time display them on the website. I have this code created in the controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
Class Subir extends CI_CONTROLLER
{
function __construct()
{
parent::__construct();
}
public function index()
{
$url = base_url('subir');
require_once './application/libraries/google-api-php-client/src/Google_Client.php';
require_once './application/libraries/google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('aqui va mi id');
$client->setClientSecret(' aqui va mi secret id');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
$files= array();
$dir = dir('./public/files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
$dir->close();
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 = './public/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;
}
$datos['files'] = $files;
$datos['url'] = $url;
$this->load->view('subir',$datos);
}
}
?>
and this is the code of the view
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Google Drive Example App</title>
</head>
<body>
<ul>
<?php foreach ($files as $file) { ?>
<li><?php echo $file; ?></li>
<?php } ?>
</ul>
<form method="post" action="<?php echo $url; ?>">
<input type="submit" value="enviar" name="submit">
</form>
</body>
</html>