I try to resolve my problem with resource path in Php. In file header.php I include scripts and stylesheets, but when I require_once('../templates/header.php'); in file login.php from views directory I get 404 error code on all my resources, because all files must be in subdirectory of views direcroty. How I can solve this problem?
Path:
/var/www/reg/templates/header.php - path to header
/var/www/reg/views/login.php - path to login
/var/www/reg/js/script.js - path to js
/var/www/reg/css/style.css - path to css
according to your file structure
inside header.php
try this way:
<link href="/reg/css/style.css" rel="stylesheet" />
<script src="/reg/js/script.js" ></script>
As I have tested it works for me.
This is how I'd probably do it, not sure if best practice, but similar to the way wordpress works, IE
In your a index file (in the reg/ dir or in a config file if you have one):
define('SITEPATH', dirname(__FILE__).'/');
Then you can require files like this:
require_once(SITEPATH . 'templates/header.php');
Then when you're calling your resource files, you can do something similar this:
<script src="<?php echo SITEPATH; ?>js/script.js"></script>
But, imo, backend stuff shouldn't really be on template files, but because your directories are different to how I'd lay them out.
Related
Disclaimer: I haven't got a clue what I'm doing with PHP I'm just playing around with it.
I have my css file in a folder named CSS and then my header.php and footer.php in the main site folder. If i include the header.php in other directories I am just using:
<?php include('../header.php'); ?>
I know this isn't the way to do it however I don't know how to configure it probably (with a config.php file etc..) but my issue is, once the header's included in files in any directory of course it will look for the css/main.css file in that folder so I've tried doing the following:
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']. '/JAGS/css/main.css' ?>" />
When I use the php line in the body it displays the path
E:/xampp/htdocs/JAGS/css/main.css
but if I use it there in the link tag then doesn't work.
What seems to be my problem other than the fact I'm clueless with PHP. Is there something else I should be using? Is there something I need to do in my xampp config files?
Edit: By "doesn't work" I mean the styles are not being applied.
Edit 2: Inspecting shows the following:
<link rel="stylesheet" href="E:/xamppp/htdocs/JAGS/css/main.css">
I know there's an extra p on the end of xampp, this is actually what I have the folder named. Is it because it's not saying "localhost/JAGS/CSS/main.css"? If so what would be the reason for this?
Edit 3: Console shows error below:
Not allowed to load local resource: file:///E:/xamppp/htdocs/JAGS/css/main.css
Edit 4: Not using Laravel
Thank you
Why do you require the document root, you should just place a dot in front of it and set the base url in a meta tag.
<base href="yourdomain.com">
<link rel="stylesheet" href="./JAGS/css/main.css'/>
like the public folder given by laravel default i tried to create something like that for easy make up of my css, js, picture and so on. I tried configuring it like this. In my application/helpers/utility_helpers.php
<?php
function asset_url()
{
return base_url().'assets/';
}
?>
I have my assests folder along with application, system, user_guide etc.
i confugured the autoload function of application/config as :
$autoload['helper'] = array('url','utility');
when i hit any url in browser now, it says
An Error Was Encountered
Unable to load the requested file: helpers/utility_helper.php
how can i make it and can you explain me if i need to specify root for that helpers also.And if not can i simply access the assets like this:
<img src="assets/base_components/build/banner.png">
in my view
In fact the answer is very simple, you only need to put ../ before every file that is under assets folder, because the root of the codeigniter is in application folder, but how you can't put the assets folder there and for the best practices, let the structure of the project like below and apply this solution to the path of the files:
Website Folder structure
|---|application\
|-------(CodeIgniter application folders...)
|---|assets\
|-------css\
|-------images\
|-------js\
|-------robots.txt
|---|system
|-----(CodeIgniter system folder...)
|index.php
Path of the files
ex.:
CSS
<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
<link href="../assets/css/plugins/font-awesome.css" rel="stylesheet">
IMAGES and others
<img src="../assets/images/build/banner.png">
JAVASCRIPT
<script src="../assets/js/jquery-2.1.4.js"></script>
<script src="../assets/js/plugins/jquery.scrollSpeed.js"></script>
Note.:
Always remember to applay the best practices to the code too (this will help for the performance of the the page too), like below:
<html>
<head>
meta tags
favicons
css files styles
</head>
<body>
page structure code
javascript files
</body>
</html>
Yo can do this instead:
<img src="<?= base_url('assets/base_components/build/banner.png') ?>">
Your code looks ok, it should work.
According to the error
Unable to load the requested file: helpers/utility_helper.php
there is no utility_helper.php file in application/helpers/ folder. Check if the file exists in that folder or if you don't have a typo in the file name.
I will have multiple folders/modules to access common files. But accessing them seems to be big deal for me!
I did gone through this link to understand the relative positioning and managed to solve some . But not all. Reference: Relative URL's/paths in php
My folder structure is as below:
Website runs on root folder:
/(index|ajax).php
and then the subfolders:
/css/style.css
/img/*.(jpg|png|gif)
/inc/(header|footer).php
/js/*.js
/registration/(ajax|getsubjects|response|success).php
Now, this is how I included files in the index.php page(this displays correctly, meaning, style,css,js,config all accessible)
<?php
include('inc/header.php');
?>
content here
<?php
include('inc/footer.php');
?>
This index page will have to fetch getsubjects.php, response.php and then finally land in success.php.
The success.php need some styling whereas the previous two were only for processing.
So now in the success.php I access header and footer as below:
include('../inc/header.php');
include('../inc/footer.php');
But this doesn't apply any styling!
inside header.php and footer I include files like this:
<link rel="stylesheet" href="./css/style.css">
<script src="./js/script.js"></script>
How should I include the files here please?
./css/style.css means from current directory and would achieve the same result as css/style.css. The easiest answer is to determine what the base path of your application is and use that. For instance, if your application is running as http://myapp.com, then you could set all your front-end paths to /css/style.css. If your app runs in a subdirectory, such as http://example.com/myapp, then your paths would be /myapp/css/style.css.
This does not apply the same on the PHP side. For them, you should really use document-relative paths. Having a PHP file that you include in multiple places in your app, the contents of which having something like include('../myDoc.php');, can lead to complications as the path isn't based on the included document's path, but rather the including. So using document-relative paths, you get around this include(__DIR__ . '/../myDoc.php');. Just something to consider if your app grows.
Your PHP-includes seem to be correct. But in your HTML you need to change the linking to the CSS and JS Files (maybe even to your images).
You could use absolute paths:
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
the leading dot makes your paths relative to the HTML-Document, so if they are linked from a document in a subfolder, they point to a wrong location.
Including files with
<?php
include("page1.php")
?>
put the code (or content) from page1 into the caller page.
So you may have to detect from where your pages are called, or try absolute links (beginning by /)
I hope I answer you question correctly.
I am trying to include a PHP file in another directory.
Here is my file structure
settings\
manage.php
website \
index.php
main.js
main.css
For manage.php
echo 'Welcome to the manager! Here is your website:';
include('../website/index.php');
index.php
<script src='main.js'></script>
<link rel="stylesheet" type="text/css" href="main.css">
So, when I load manage.php, I do not get main.js or main.css. How can I get this to work? Maybe include is not the right way to go? I cannot modify anything in the website folder.
[I know iFraming is a possible solution but I'm hoping to get another answer]
Since you cannot edit /website/ content you should could try this ugly code for a startup.
Add following just before include statement in your manage.php
echo('<base href="../website/">');
If it works for you, then you can think of sending a correct header with PHP before including a html file, instead of directly echoing a base tag.
Please consider comments of jeroen as down-to-earth solution and use frames
The problem here is that when the browser loads /settings/manage.php it will make requests for /settings/main.js and /settings/main.css which don't exist
You probably need to change your html in index.php to something like this:
<script src="/website/main.js"></script>
<link rel="stylesheet" type="text/css" href="/website/main.css">
Note I've made some assumptions about your URLs based on your directory layout so you may need to adjust my solution to make it work for you
Based on the comment below the question: If you want to include some functions / code for your users (instead of the other way around; you including user's stuff in your code), you should look into the auto_prepend_file directive.
Basically, you specify in your php.ini file that you want to prepend (as a require) a certain php file before the main file.
Edit: As you don't have access to php.ini but you can use a .htaccess file, you can put this in your .htaccess:
php_value auto_prepend_file "/path/to/your/file.php"
Where and how do I include .js files in Views in CodeIgniter?
I have tried this:
<script type="text/javascript" src="system/application/libraries/jquery.js"></script>
As I figured that the index.php is the one that is loading the views, so I'm guessing that every time a page is loaded the current dir path is at root because index.php is located at root. Is this true?
The above line doesn't so I am doing something wrong. I have a CodeIgniter project. The path is like this:
localhost/CodeIgniter/system/application
so which path should I use to include my jquery.js file which is located in
localhost/CodeIgniter/system/application/libraries
when I want to load the jquery.js file in a View located here:
localhost/codeIgniter/system/application/views/till_view.php
There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.
<base href="<?=base_url();?>">
<img src="images/logo.gif" alt="Logo" />
Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".
It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a
$this->load->view("viewname");
The file "viewname.php" can then include the .js files, just as a normal .html (or .php) file would:
<script src="/url/to/javascript.js" />
You may want to create a default view or a "header" view that includes some or all of the (common) .js files for your project.
-CF
First question:
It depends if you use absolute or relative urls.
Absolute urls go from the root of your domain. Relative urls are loaded relative from the current directory (including the url segments).
Second question:
It's best to use an absolute URL. Because of the pretty urls, it's not recommended to use relative urls.
The easiest way is to use the url helper and then use the site url function like this:
$this->load->helper('url');
echo site_url('system/application/libraries/jquery.js');
Ps. I recommend to put things like javascript and images outside of the CodeIgniter directory.
base_url() always works fine for me
Here is a solution specifically relevant to the OP which I didn't think anyone else provided.
<script type="text/javascript" src="<?= base_url() ?>libraries/jquery.js"></script>
So, base_url() will provide a path direct to the root of your site.
<script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>
base_url() will provide the url of the site
The easiest way you can directly access the file:
<img src="http://localhost/ci/you_dir/your_img/your_image.png" />
where ci is for codeigniter.