Codeigniter: Make uploads accessible - codeigniter

I'm using the upload class to allow users uploading file to the server.
Those files (e.g. profile images) should be accessible in the browser. Right now, the files stored under /application/uploads which of course cannot be accessed via browser.
Is there a way to make those uploaded files accessible via htaccess?
The only way I can think of, would be moving the files into the /public folder after being uploaded to /application/uploads
What's the best case to handle this?

your folder structure should be like :
---application
--- controllers
--- helpers
--- views
---asset
---system
---uploads
And Whenever you want to fetch/show image use
<img src="<?php echo base_url('uploads/'); ?>" >

Save the files in outside i.e. in public/uploads folder
Create .htaccess file in that folder
Add following line to .htaccess to secure the folder
Options -Indexes

Related

Image path issue when uploading Laravel 6 project to live server?

I uploaded my Laravel 6 blog to a live server. I put the content of the 'public' directory in the "public_html" folder and I created another directory outside the "public_html" where I uploaded the rest of the files and folders.
Everything seems to be working ok, except when I create a new category or post the images are not uploaded. I guess it's a path problem.
I think I need to modify the path in the controller, like here
$category = Image::make($image)->resize(1920,479)->stream();
Storage::disk('public')->put('category/' . $imagename, $category);
Since I no longer have a directory called "public", what should I put there? How do I change this path?
My css and js files were linked like this on localhost:
<link href="{{ asset('public/assets/frontend/css/bootstrap.css') }}" rel="stylesheet">
when I just left out "public/" they were working fine on the live, but if I leave out the "public" in the controller code, it's not working.

keeping safe assets folder in codeigniter

Hi i have this assets folder which has the following folder inside of css, images, uploads, and js folder i put the assets folder outside application. Im wondering that how could i keep it safe so that when users tried to type in the url like for example http:/test.com/assets it will redirect to application folder or somewhat it will says page not found. Cause ive noticed that when i type in the url http://test.com/assets it will go to the assets folder and which is vulnerable and people could see all the folders in the assets folder. can someone help me figued this thing out? Any help is muchly appreciated.
if you want to secure you folder then do one this create html file like this
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>
and save it as index.html and put it on your assets folder whenever some try to access your url http://test.com/assets then this index file will execute and its shows Directory access is forbidden.

How to link assets(images,stylesheet etc) in Views for CodeIgniter 2.1?

I am using CodeIgniter Version 2.1 & trying to link assets like images,stylesheets, javascript files etc in my views by using, header.php:
<link href="<?php base_url();?>css/style.css" rel="stylesheet" />
my controller code, calls the view:
<?php
class Main extends CI_Controller{
public function index() {
$this->load->view('header');
}
The view file from which I am trying to load the asset is located
../application/views/header.php.
the css file is loaded:
../application/views/css/style.css
this does not work. I get 404 - Page not found error.then, I tried moving css/style.css outside ../application directory in webroot. To my surpise, having assets in webroot(outsite ../application/views) seems to work nicely.
Now,
My Question is
Is having our assets directly in webroot, outsite ../application directory right approach? If YES/NO, then why?
If having assets directly in webroot is a good idea, than should I move ../application/views directory in webroot as well? How?
PS: I am new to CodeIgniter framework, so unaware of the best practices
If you see this, the section where .htaccess is used to remove index.php from the urls, that has a rewrite condition:
RewriteCond $1 !^(index\.php|images|robots\.txt)
In this line, images is a folder that will be ignored while mod-rewriting. So, you can replace it by assets, put all your directly linking files there, and happily use them.
RewriteCond $1 !^(index\.php|assets|robots\.txt)
EDIT: my bad, hadn't seen the date the question was asked. nevertheless, should be useful for someone.
It is best to put your assets in the webroot folder. When requesting assest from the server (depending on your setup), it will start from the root directory and work it's way down.
http://yoursite.com/application/views/css/mystyles.css
if it's in the root you only need to go from there
http://yoursite.com/css/mystyles.css
Although it might be worth putting them all inside a folder (/assets) to keep them contained, as well as the ability to write a more effective rewrite rule (if you are trying to remove the index.php from the url) to ignore the single folder instead of all the individual folders (/css, /js, etc)
As for the views folder, it's best if you leave it in the application folder as CodeIgniter has a built in loader that automatically checks /application/views for the view file when you use the code $this->load->view('myview')
Although there are way to move the views folder, if you are new to CI it's probably best to leave it there for now.
For linking css you can do something like:
echo link_tag('css/mystyles.css');
Check this for ref: http://codeigniter.com/user_guide/helpers/html_helper.html

where should I add external file to view in CI

I'm trying to make view page in CodeIgniter,So I create it and in Controller it loads complete.
but when I add images and jQuery to it,they will not be loaded.
I made sub folder in view folder by name of Files and add to view for e.g like that
img src="Files/01.jpg" but it will not be shown.
where should I place them?
I usually make a folder called files in the main directory (where system and application reside), and then link to them using base_url().
In your case, this would become
<img src="<?= base_url(); ?>/files/01.jpg" />
Hope that helps.
When your view loads, the file paths will be relative to the directory the CodeIgniter index.php file is located in. If this is also the root directory of your site, you can refer to it in this way: <img src="/Files/01.jpg" />

Image display codeigniter fails

I can upload image to the directory but can not disply it on the page.
Tried using static html code and codeigniter img() function, both are not working.
Here is my code:
<image src="./applications/views/uploads/image.jpeg" />
and
$this->output->set_header("Content-Type: image/jpeg");
$data['image'] = $this->load->file('../uploads/taurus.jpeg');
the first code shows me broken image the second one shows Unable to load the requested file: taurus.jpeg
<image src="./applications/views/uploads/image.jpeg" />
By default, CodeIgniter denies access to the application folder which is why I suspect this isn't working.
$this->output->set_header("Content-Type: image/jpeg");
$data['image'] = $this->load->file('../uploads/taurus.jpeg');
I suspect this isn't working because your path is incorrect. The path is relative to the index.php file in the root of your application, not the file this code is in. Secondly, $this->load->file() sends to file contents to the browser so this wouldn't work anyway.
A solution would be to move your uploads folder to a web accessible location. For example, place the uploads folder into the root of your application - the same folder as your index.php file. Then you can display your image like this:
<image src="/uploads/image.jpeg" />
Just get rid of the dot (.) in the URL so your src url reads: "/applications/views/uploads/image.jpeg"

Resources