composer added and first version of upload helper

parent 3757e78e
uploads/*
vendor
\ No newline at end of file
......@@ -23,7 +23,8 @@ defined('BASEPATH') OR exit('No direct script access allowed');
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'https://wbt-x-amr.appspot.com';
$config['base_url'] = ENVIRONMENT == 'production' ? 'https://wbt-x-amr.appspot.com/' : 'http://localhost/wbt-web-app/';
/*
|--------------------------------------------------------------------------
......@@ -378,8 +379,8 @@ $config['encryption_key'] = 'GLn08ulSFJM3dTx4Q17o7hdFjlSEeX9I';
|
*/
$config['sess_driver'] = 'memcached';
$config['sess_save_path'] = 'localhost:11211';
$config['sess_driver'] = ENVIRONMENT == 'production' ? 'memcached' : 'files';
$config['sess_save_path'] = ENVIRONMENT == 'production' ? 'localhost:11211' : NULL;
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_match_ip'] = FALSE;
......
......@@ -72,8 +72,10 @@ defined('BASEPATH') OR exit('No direct script access allowed');
*/
$active_group = 'default';
$query_builder = TRUE;
/*
$db['default'] = array(
$db['default'] = ENVIRONMENT == 'production' ?
array(
'dsn' => '',
'hostname' => '35.230.156.110',
'username' => 'appdbuser',
......@@ -93,9 +95,8 @@ $db['default'] = array(
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
*/
$db['default'] = array(
) :
array(
'dsn' => '',
'hostname' => 'localhost:3308',
'username' => 'appdbuser',
......
<?php
class Upload extends CI_Controller {
function __construct () {
parent::__construct();
}
public function upload() {
$this->load->helper('upload');
if (ENVIRONMENT == 'production') {
$url = upload_to_bucket('wbt2020', 'uploads');
} else {
$url = upload_to_local('./uploads');
}
$this->load->view('upload', array('img_url' => $url));
}
}
\ No newline at end of file
<?php
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
// Store file in $path in the bucket $bucket_name
function upload_to_bucket ($bucket_name, $path) {
if(isset($_FILES['file'])) {
// Authentication with Google Cloud Platform
$storage = new StorageClient();
$bucket = $storage->bucket($bucket_name . '/' . $path);
// Upload a file to the bucket.
$file = pathinfo($_FILES['file']['name']);
$prefix = uniqid('uploaded_', true); //generate random string to avoid name conflicts
$filename = $prefix . "." . $file['extension'];
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r'),
['name' => $filename]
);
return ('https://storage.cloud.google.com/'.$bucket_name.'.appspot.com/'.$path.'/'.$filename);
}
}
// upload a file to local server filesystem
function upload_to_local ($path) {
if(isset($_FILES['file'])) {
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20480; // Max file size: 20480 = 20M
$config['max_width'] = 2048;
$config['max_height'] = 2048;
$config['encrypt_name'] = TRUE;
$ci =& get_instance();
$ci->load->library('upload', $config);
if ( ! $ci->upload->do_upload('file'))
return NULL;
else
return base_url('uploads/').$ci->upload->data('file_name');
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ $allowOnly = array(); // actions only available to certain groups of users
// Public actions
$allowAll['auth']['login'] = TRUE;
$allowAll['pages']['view'] = TRUE;
$allowAll['upload']['upload'] = TRUE;
// Controlled access
$allowOnly['user']['auth']['logout'] = TRUE;
......
<form id="fileForm" action="<?= base_url('index.php/upload/upload') ?>" method="post" enctype="multipart/form-data">
<div class="form-row">
<br/>
<div class="form-group">
<input type="file" name="file">
</div>
</div>
<br>
<button type="submit" class="btn btn-lg btn-primary"> Upload </button>
</form>
<?php if (isset($img_url)) {
?>
<img src="<?= $img_url ?>"></img>
<?php
}
?>
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment