fixed all errors on local upload, now testing cloud uploads

parent ad1d414d
uploads/* uploads/*
vendor vendor
index.php
...@@ -523,3 +523,15 @@ $config['rewrite_short_tags'] = FALSE; ...@@ -523,3 +523,15 @@ $config['rewrite_short_tags'] = FALSE;
| Array: array('10.0.1.200', '192.168.5.0/24') | Array: array('10.0.1.200', '192.168.5.0/24')
*/ */
$config['proxy_ips'] = ''; $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 <?php
/*
| This is an example controller to show how to use upload helper
| amontejo@ujaen.es - 2020
*/
class Upload extends CI_Controller { class Upload extends CI_Controller {
function __construct () { function __construct () {
...@@ -8,12 +11,8 @@ class Upload extends CI_Controller { ...@@ -8,12 +11,8 @@ class Upload extends CI_Controller {
public function upload() { public function upload() {
$this->load->helper('upload'); $this->load->helper('upload');
//if (ENVIRONMENT == 'production') { $fileurl = upload_file();
// $url = upload_to_bucket('wbt2020.appspot.com', 'uploads'); $this->load->view('upload', array('img_url' => $fileurl));
//} else {
$url = upload_to_local('./uploads');
//}
$this->load->view('upload', array('img_url' => $url));
} }
} }
\ No newline at end of file
<?php <?php
/* /*
require 'vendor/autoload.php'; | This is the upload helper
use Google\Cloud\Storage\StorageClient; | amontejo@ujaen.es - 2020
*/
// 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);
// Upload a file to the bucket. /*
$file = pathinfo($_FILES['file']['name']); This function will check if there is a input POST 'file' item.
$prefix = uniqid('uploaded_', true); //generate random string to avoid name conflicts If so, it will save the file to the path specified by the
$filename = $path . '/' . $prefix . "." . $file['extension']; 'upload_path' config parameter set at config/config.php
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r'),
['name' => $filename]
);
return ('https://storage.cloud.google.com/'.$bucket_name.'/'.$filename);
} 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_file () {
function upload_to_local ($path) {
if(isset($_FILES['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['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20480; // Max file size: 20480 = 20M $config['max_size'] = 20480; // Max file size: 20480 = 20M
$config['max_width'] = 2048; $config['max_width'] = 2048;
$config['max_height'] = 2048; $config['max_height'] = 2048;
$config['encrypt_name'] = TRUE; $config['encrypt_name'] = TRUE;
$ci =& get_instance();
$ci->load->library('upload', $config); $ci->load->library('upload', $config);
if ( ! $ci->upload->do_upload('file')) if ( ! $ci->upload->do_upload('file'))
return NULL; return NULL;
else 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"> <form id="fileForm" action="<?= base_url('index.php/upload/upload') ?>" method="post" enctype="multipart/form-data">
<div class="form-row"> <div class="form-row">
<br/> <br/>
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
* *
* NOTE: If you change these, also change the error_reporting() code below * 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