where should I add external file to view in CI - model-view-controller

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" />

Related

Codeigniter 4 assets PATH configuration for Modules

I m creating different directories for Role-based access in Codeigniter 4. I have created the Modules directory inside app/ i.e (app/modules/admin). I have assets for the admin. I want to load it in the header file of view. I have tried it with the base_url().'/app/modules/admin/assets/filename.xyz' but load the complete file in the link but doesn't show me the assets. How can, I load these assets in the same directory. Please see the below code
<link rel="stylesheet" href="<?php echo(base_url().'/app/modules/super/assets/'); ?>plugins/fontawesome-free/css/all.min.css">
All public files including assets should be placed inside your public folder and not your app folder. Your app folder shouldn't be visible to the public.
With this in mind what I would do would be create a simular structure in your public folder.
public/modules/admin/css
public/modules/admin/js
public/modules/admin/img
Then if you have an asset called styles that would be in:
public/modules/admin/css/styles.css
This asset should be called using the base_url function from the uri helper.
<link src="<?php echo base_url('modules/admin/css/styles.css'); ?>">

Codeigniter: Make uploads accessible

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

how to put <img> tag in header.php in Wordpress

i have used the following code but it didnt work
/images/ncr.jpg" alt="ncr.com" />
I have pur my image in the images folder of twentyeleven child theme
can anyone help
Thanks in advance..
The theme folder URL can be found at /wp-content/themes/twentyeleven.
Your header file is actually included at the root level, and as such relative paths won't work.
WordPress provides a function to access your current theme's directory, and as a result you should use this code:
<img src="<?php echo get_template_directory_uri(); ?>/images/ncr.jpg" alt='ncr.com'>

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

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