I can't show images in codeigniter project - php

I'm trying to display images that I upload to my FTP but I don't know what I'm doing wrong.
First of all, I'm saving "uploads/images/".$image['file_name'] in my DB to know the relative URL of the file.
Later, in the target view, I declared the helper('url') and helper('html') to use without any problem the next functions:
img()
base_url()
Having all this done, I call the image as img(base_url($object['image_url'])) where object['img_url'] is called from my DB with the correct structure.
I tried to use the <img> tag too, but the src doesn't loads correctly.
The url that I'm getting from the DB is the correct one, but if I load it at the browser, it appears a 404 error because there is no "uploads" class with "images" method. My uploads folder was created at root level of the project.
What I'm doing wrong? I had to change something in the configuration of the project?

CodeIgniter routes all requests to index.php and looks for a controller. Try adding this to .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Do you have .htaccess. Example:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|fonts|products_images|shops_images|uploads|assets|robots\.txt|migration)
RewriteRule ^(.*)$ /dobrioferti/index.php/$1 [L]

Related

how to work image from 2 level folder

I have a problem with my queries doesnt work the image of the source, so i have root/travel-page.php in the root folder and on the query i want put a image from root/travel/imagini/picture.jpg .. i putted all the html link http://exemple.com/travel/imagini/picture.jpg , but doesnt work
the page.php i redirectionat using htacces in travel/queryLink.php
So my question is if i can do something to make those image to appear if is 2 level of folder diference between the page and the image
if i try to call the image from root/images/picture.jpg - is work.. because is only a level between thoose.
RewriteRule ^travel/([0-9a-zA-Z_-]+) ./page.php?pid=$1 [NC,L]
I FIXED the problem, maybe someone will have the same problem with the image what not show after the RewriteRule
i needed to add RewriteCond %{REQUEST_FILENAME} !-f
so the right code is this...dont forget to request the filename how i did it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^travel/([0-9a-zA-Z_-]+) ./page.php?pid=$1 [NC,L]

.htaccess is reloading some files twice

My website is a custom made PHP website. I recently made it SEO friendly by copying the .htaccess file from wordpress and modify it a little. The problem is that some pages are realoaded twice especialy pages that have more than 1 backslash like download/something/ . I have noticed this when tracking pageviews one pageviews is counted as twice and i've done a lot of research regarding this and the pageviews are working good it's a very simple function that inserts a new row each time you view a page.
Things to keep in mind:
My website is inside some folders 'https://localhost/simbyone/sim/index.php'.
I don't want to use any GET variables i will have my variables from the URL string
I want .htaccess to look for existing directories located inside 'https://localhost/simbyone/sim/' and if it doesn't find any open index.php with all the strings attached something like 'localhost/simbyone/sim/blabla/' but inside index.php
everything works exactly as i said above the only thing that doesn't do right is that it double loads some pages
this is my .htaccess file:
RewriteEngine On
RewriteBase /simbyone/sim/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /simbyone/sim/index.php [L]
Thanks in advance to anyone that will help me.
Place the .htaccess inside your base folder (in your case, simbyone/sim).
Just put this in the file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php
This will rewrite and request that is not an existing file or folder to the index.php file.
There you can use $_SERVER['REQUEST_URI'] to evalute the request (keep in mind that the prefix "/simbyone/sim/" will be present there.

What happens when a user types only the domain name and or port number in the browser's address bar when accessing a site built with codeigniter?

I know that in Codeigniter, the default controller is always called if a controller is not specified in the url. Furthermore, if a function is not specified in the url, the index function of the controller will be called by default. However when I enter the following urls in the browser address bar I get different results for each:
http://localhost/appfolder/
And
http://localhost/appfolder/index.php/defaultController
For the first url, it seems that the user is not logged in even if indeed the user is logged in. The second url works fine and does the correct thing whether the user is logged in or not, that is, it excecutes the index function of the default controller. It seems to me that the first url doesn't excecute the index function of the default controller. I'm racking my brain trying to figure out why entering the two urls produces different results because according to what I know, they're supposed to have the same results; i.e they're both supposed to call the index function of the default controller.
I'm sort of a newbie in using codeigniter (have used it for about 2 and half months now) so any help would really be appreciated.
This is an edit:
I know most of the comments here are about using a htaccess file but isn't it supposed to work the same way whether index.php is in the url or not? Does anyone have any other suggestions on what I can do to pinpoint the problem?
If you want to access the site through the url http://localhost/appfolder instead of http://localhost/appfolder/index.php/defaultController you will need to set up a .htaccess file in your sites webroot folder (the folder containing index.php). This is pretty much what I use on my site (customized for your setup):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /appfolder/index.php/$1 [L]
Alternative
RewriteEngine On
RewriteBase /appfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Similar question with plenty of discussion and other methods:
How to remove "index.php" in codeigniter's path
Set the default controller in the routes config file:
$route['default_controller'] = 'Blog';
From the docs:
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable.
Hey use this one in your htaccess file i think it will work............
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Url rewriting HTML and CSS paths

I have the following .htaccess file in the root of my application:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This basically just convert paths such as index/var=value/var2=values2/ into index?url=index/var=value/var2=values2/ and in index.php they are elaborated to fetch $controller which is the name of the controller called (in this case index) and the $get which is the substitute for $_GET. (In this case it contains: $get['var'] = 'value' e $get['var2'] = 'value2').
Notice: In the root file, where the .htacces file and the index.php are, we have also the application/ folder (which contains controllers, views, and models), the system/ folder which initialize the framework and the public/ folder which should contains every image/javascript/css/video/audio file which can be accessed.
The .htaccess allows me to access directly every existing file both images and css. Which is great. The only problem that occurs is when the path to that image/javascript/css/audio/video file is inside the HTML or CSS files. So that when you call (*1) <img src="public/img/image.jpg"/> or when you call (*2) background-image: url(public/img/image.jpg); it just doesn't work.
That could be "solved" using absolute paths such as http://dominio.com/public/img/image.jpg but it is tricky when it comes to javascript files. And anyway is not a solution at all, but just a way to bypass the problem.
How could I make (*1) and (*2) work?
Have you tried this path?
/public/img/image.jpg
EDIT/ADD
didn't test this, but you can try
RewriteCond %{REQUEST_URI} ^/public [OR]
RewriteCond %{REQUEST_URI} ^/other_folder [OR]
RewriteCond %{REQUEST_URI} ^/more_folders
RewriteRule (.*) /project/$1
OR, have index.php process everything, and the very fist lines are a check to see if the called files exists based on the current path:
//untested code
$url = str_replace('..','',$_GET['url']);
if(file_exists(dirname(__FILE__).'/'.$url)){
//redirect to that file or serve it with PHP
exit(0);
}
background-image: url(public/img/image.jpg);
will look in public/img path relative to the CSS file.
What you should probably be using is ../img/image.jpg
or /public/img/image.jpg

Url rewriting broke link to css

I'm using the following setting for url rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In index.php is parse $_GET['url'] so that in the following examples:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
So that the first parameter(? i don't know how to call it) is the page name then the following couple of parameters are like "keys/value".
Now when i browse ROOT/ the link to stylesheets that are inserted inside the html of the page and the page are shown correctly.
I fi browse ROOT/index (which is the same as ROOT/) it shows the page (with contents and other stuff) correctly but the links (even if in the html structure are correctly written) to stylesheets are not loaded. And i can see that from the fact that my page has no css at all when i load it.
How can I fix this?
EDIT
The css file's path is as follows:
project/view/css/common.css
The file where is it included is in
project/public/index.php // the one with .htaccess and rewrite rules
This brings me to make a link (inside the index.php) such as
../view/css/common.css
But this works different depending on how the url seems. For examples:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
Comment doesn't let me format the code, can you please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Update
You can try something like this in the <head> section of your pages to support relative path for your image/css/js files referenced on that page:
<head>
<base href="http://www.example.com/static-files/" />
</head>
I think you have two problems (as the given answers didnĀ“t solve your problem yet...):
You are rewriting all file-names so when the browser is requesting css/index.css your rewrite transforms that to index.php?url=css/index.css. #cOle2's answer should solve that.
You are not using absolute paths for your css files. The server is translating the requested page like /user/id/1/name/bobby to /index.php?etc.... but when the browser requests for example the css file that is something like css/index.css in your code, it will actually request /user/id/1/name/bobby/css/index.css and that file does not exist. You can solve that by using only absolute paths to your external files (css, js, images, etc.) like /css/index.css.
Edit: Based on your comments, both your paths are relative and your css is not accessible by the browser, so you need to:
Move the css to the project/public directory (like project/public/css)
Use absolute paths to your css like /css/index.css.
Make sure the urls to your external files are not rewritten by the server.
You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.
# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
Edit: This is how I would apply it:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Categories