Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Arturo Montejo Ráez
/
wbt-web-app
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
69701844
authored
Apr 22, 2020
by
Arturo Montejo Ráez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fixed all errors on local upload, now testing cloud uploads
parent
ad1d414d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
44 additions
and
37 deletions
.gitignore
application/config/config.php
application/controllers/Upload.php
application/helpers/upload_helper.php
application/views/upload.php
index.php
index.php.bak
.gitignore
View file @
69701844
uploads/*
vendor
index.php
application/config/config.php
View file @
69701844
...
...
@@ -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
application/controllers/Upload.php
View file @
69701844
<?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
application/helpers/upload_helper.php
View file @
69701844
<?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
application/views/upload.php
View file @
69701844
<?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/>
...
...
index.php
View file @
69701844
...
...
@@ -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
'
);
/*
*---------------------------------------------------------------
...
...
index.php.bak
0 → 100644
View file @
69701844
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment