I use the mudule AltoRouter with php -t to allow just the acces at one folder. But since I use it, i can't refer to other files in HTML. For exemple, in can't acces my CSS file, or my pictures that I use. I says "GET /style/style.css - No such file or directory". Here all my files.
My default file is in pages/includes/default.php.
<link rel="stylesheet" href="../../style/style.css"/>
I try with
direname(__DIR__) . "style/style.css"
in php, with "./", with a constant, but nothing work.
Can someone help me ?
Related
This is the file path
<link rel="stylesheet" href="../css/bootstrap.min.css">
I tried all the possible ways but couldn't fix the problem. Browser console says err_abborted 404
As said above it depends on the structure of your files how you should write the filepath (and is it linked in the head?). But have you tried to link it to BoostrapCDN, without the ../ like in this 'Get started documents example'? https://getbootstrap.com/docs/4.5/getting-started/introduction/
Hope it works out!
I am trying make bootstrap.php file that gets called everytime before i open a file, like my css. And i do this since i use the header in every page i need to have all of the links working without having to do multiple header depending on where in my folder structure i am. And this is the error:
Not allowed to load local resource: file:///C:/xampp/htdocs/PHP/assets/css/bootstrap.css
And here is my file structure.
index.php
include ('bootstrap.php');
include (FOLDER_MAIN_HTML.'header.php');
bootstrap.php
define ('FOLDER_ROOT', 'C:/xampp/htdocs/PHP/');
define ('FOLDER_CSS', FOLDER_ROOT.'assets/css/');
header.php
<link rel="stylesheet" href="<?php echo FOLDER_CSS;?>bootstrap.css">
Dont know if its neccesarry but this is my httpd.conf in xampp
DocumentRoot "C:/xampp/htdocs/PHP"
<Directory "C:/xampp/htdocs/PHP">
So thats what i have but it doesent work. Even when when i switch the root folder to __DIR__ it still says no allowed. So do i need a htaccess file, and if so how do i do that? Or is there some other way.
And yes i have searched everywhere for a fix, and the code i have right now is from another post, from here.
I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.
After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.
This is how it looks:
The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path.
Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.
So my question is, how to fix it?
I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';
P.S:
israelrescue is the name of the folder I am using in my localhost installation for this website.
Nothing seems to work, any ideas?
Thanks!
Answers:
It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think #MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox
Looks like this site was not written to be run from within a
sub-folder. Easiest way to work around that would be to set up a
VirtualHost for this project in your local server, so that you can
load it directly via a local domain name, without any additional path.
(And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.)
– CBroe
You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>
becomes
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>
Should work like this
I have a php script in a folder "root/test/index.php" which calls another php script located in "root/app/run.php"
In the run.php file I change the php's working directory with a call to chdir to simplify my require_once paths
All the php includes work fine, however HTML paths are totally messed up.
To make my style.css works I have to use a path relative to "root/test/" even if my style.css is in "root/app/".
This works:
<link rel="stylesheet" type="text/css" href="../app/style.css">
and that goes on for all HTML sources like img etc.
How can I change HTML working directory too?
I looked in php/HTML documentation, but I have find no good solution.
Any clue?
Thanks in advance.
HTML don't know anything about the working directory of your PHP script.
The HTML is sent to the client who can't know what was the working directory.
You could add some redirection in an .htaccess file (if you are using apache) to redirect root/test/ to root/app or something like that.
Here is how it works :
You browser ask for /root/test/index.php
Your server handle all his things and then output HTML file
Your browser parse the HTML file
If it find something like that it will search the image here : /root/test/images/cat.png because you are viewing the /root/test/index.php file and you provide a relative path
Add a comment if you have more questions.
I have a PHP class that creates a header for my html file.. It's included, and that file is included again.. So I thought that the dirname(__FILE__) function could work.. But it says it can't find the stylesheet..
I'm using mamp on os x, and when I take the path that I get from dirname(__FILE__)./../stylesheets/stylesheet.css into the terminal, the file is found.. I'm pretty sure the path is correct.
What can be the reason for this? I use dirname(__FILE__) all the time when I include files, that works..
Thanks
EDIT:
Files and directories:
/data/main.php
/stylesheets/stylesheet.css
/public/index.php
In the main.php:
public function createHeader(){
`$stylesheetpath = dirname(__FILE__) . "/../stylesheets/stylesheet.css";`
`$header = "\n";`
return $header
}
The relative path should be ok.
<LINK REL=StyleSheet HREF="../stylesheets/stylesheet.css" TYPE="text/css">
An absolute path here would be absolute with respect to the document root, not the file system.
dirname(__FILE__) is appropriate for including script files server-side, but not for paths used by the client. Simply put, the link tag is an instruction to the browser to go ahead and request that file.
From you file structure, /stylesheets/ is out of the public reach, it must be placed inside the /public/ folder for the browsers to retrieve the files:
/data/main.php
/public/stylesheets/stylesheet.css
/public/index.php
Even when the file main.php is working, the file being viewed by the browser is index.php, so the relative path would be stylesheets/stylesheet.css
edit: read the question incorrectly...
use
<link rel="stylesheet" href="/stylesheets/stylesheet.css" type="text/css"/>
for css
Is this a problem with partial case-sensitivity on Mac OSX?