fixed all errors on local upload, now testing cloud uploads

parent ad1d414d
uploads/*
vendor
index.php
......@@ -523,3 +523,15 @@ $config['rewrite_short_tags'] = FALSE;
| Array: array('10.0.1.200', '192.168.5.0/24')
*/
$config['proxy_ips'] = '';
/* ------------------------------------------------------------------
| Configuration of the upload helper for enabling user uploads
|
| amontejo@ujaen.es - 2020
--------------------------------------------------------------------*/
// Destination storage path (don't forget trailing '/')
$config['upload_path'] = ENVIRONMENT == 'production' ? 'gs://wbt2020.appspot.com/uploads/' : './uploads/';
// Prefix for building public URL to the uploaded file
$config['upload_prefix'] = ENVIRONMENT == 'production' ? 'https://storage.cloud.google.com/uploads/' : $config['base_url'] . 'uploads/';
\ No newline at end of file
<?php
/*
| This is an example controller to show how to use upload helper
| amontejo@ujaen.es - 2020
*/
class Upload extends CI_Controller {
function __construct () {
......@@ -8,12 +11,8 @@ class Upload extends CI_Controller {
public function upload() {
$this->load->helper('upload');
//if (ENVIRONMENT == 'production') {
// $url = upload_to_bucket('wbt2020.appspot.com', 'uploads');
//} else {
$url = upload_to_local('./uploads');
//}
$this->load->view('upload', array('img_url' => $url));
$fileurl = upload_file();
$this->load->view('upload', array('img_url' => $fileurl));
}
}
\ 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) {
| This is the upload helper
| amontejo@ujaen.es - 2020
*/
if(isset($_FILES['file'])) {
// Authentication with Google Cloud Platform
$storage = new StorageClient();
$bucket = $storage->bucket($bucket_name);
// Upload a file to the bucket.
$file = pathinfo($_FILES['file']['name']);
$prefix = uniqid('uploaded_', true); //generate random string to avoid name conflicts
$filename = $path . '/' . $prefix . "." . $file['extension'];
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r'),
['name' => $filename]
);
return ('https://storage.cloud.google.com/'.$bucket_name.'/'.$filename);
}
}
/*
This function will check if there is a input POST 'file' item.
If so, it will save the file to the path specified by the
'upload_path' config parameter set at config/config.php
It returns the public URL path to the uploaded file, which is defined
in the 'upload_prefix' config parameter.
*/
// upload a file to local server filesystem
function upload_to_local ($path) {
function upload_file () {
if(isset($_FILES['file'])) {
$config['upload_path'] = 'gs://wbt2020.appspot.com/uploads/';//$path;
$ci =& get_instance();
$config['upload_path'] = $ci->config->item('upload_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');
return $ci->config->item('upload_prefix') . $ci->upload->data('file_name');
}
}
\ No newline at end of file
<?php
/*
| This is an example view to show how to use upload helper
| amontejo@ujaen.es - 2020
*/
?>
<form id="fileForm" action="<?= base_url('index.php/upload/upload') ?>" method="post" enctype="multipart/form-data">
<div class="form-row">
<br/>
......
......@@ -53,7 +53,7 @@
*
* NOTE: If you change these, also change the error_reporting() code below
*/
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
/*
*---------------------------------------------------------------
......
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