Jump to content

Problem with paths


forum

Recommended Posts

I can't figure out how paths work, for example I have many folders and files in them, there is a file index.php, it is the main one, in it $path = $_SERVER['DOCUMENT_ROOT'];
echo $path;
the path shows C:/xampp/htdocs/, what should be written in the files so that the styles and files work? My styles do not work and the files say that the file does not exist

Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT'] is a file system path. it is used when you reference a file through the file system on the server, such as when you require a file through php code or use a statement like file_get_contents(). these are not the same as URLs that a browser uses when it makes a request to a web server.

you would need to post examples showing what you are trying to do to get more specific help.

Link to comment
Share on other sites

for example when i go to the start page it opens and all the styles work but when i click on enter or go back to the main page it tells me that the file is not found i can never figure out how this PHP works

Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT']; =
server get the root path of this document
which really is C:/xampp/htdocs/

switch to using root-based paths (/).

require_once dirname(__FILE__) . '/dir/file.php';
/ = start at root (htdocs)
dir/ = enter folder named dir
file.php = load this file

echo '<link rel="Stylesheet" type="text/css" href="/css/styles.css" />';
/ = start at root (htdocs)
css/ = enter folder named css
styles.css = get this file

require_once dirname(__FILE__) . '/../dir/file.php';
/ = start at root (htdocs)
../ = move up one dir (now outside of root)
dir/ = enter folder named dir
file.php = load this file

 

Link to comment
Share on other sites

I simply used

define('BASE_PATH', realpath(__DIR__));

in a configuration file then on the page itself just use something like this at the top of page -

<?php
// Include the configuration file and autoload file from the composer.
require_once __DIR__ . '/../config/config.php';
require_once "vendor/autoload.php";

If you plan to use $server_name later in your code to construct paths or URLs that involve the server. Then do this in the configuration file ->

$server_name = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL)

An example :

<?php
// Get the server name and sanitize it.
$server_name = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL);

// Construct the base URL using the server name.
$base_url = "https://" . $server_name . "/";

// Construct the URL for loading the CSS file.
$css_file_url = $base_url . "css/styles.css";
?>


<link rel="stylesheet" href="<?php echo $css_file_url; ?>">

 

Edited by Strider64
Link to comment
Share on other sites

Of course I tried, but for me what is written here is not all clear, in general I solved this problem in only one way, in the file ntpd-vhosts.conf I registered the root document and then everything worked.

Link to comment
Share on other sites

  • 3 weeks later...
On 10/20/2024 at 10:39 AM, forum said:

Of course I tried, but for me what is written here is not all clear, in general I solved this problem in only one way, in the file ntpd-vhosts.conf I registered the root document and then everything worked.

 

Right, well things in "web space" ie. relative to the document root, should always be referenced relatively to the document root "/" which is equivalent in webspace to the document root.

This is because client browsers can only see what you make available to them in web space via url's.

What this means is that things like css and javascript, or images will exist in web space.

Document Root relative (web space)

Assume you have a header file you want to reference in your html (even if rendered by php) at:

 /var/www/your_project/images/header.jpg

Assume that your documentRoot for your vhost is /var/www/your_project

Then your header.jpg file should be referenced relative to the document root:

<img src="/images/header.jpg">

The same goes for any css or js files, or any other assets you want available via a direct url.

NOTE: that you should always include the beginning "/" equivalent to "document root".

This also makes sure that your code is always portable and not hard coded to a specific file system structure.

File System Paths

PHP commands like require_once/include/fopen etc. work with files on the file system.  In other words "web space" is not relevant to PHP.  PHP works with files, so it must know where the files exist. 

With that said, it is typical, that the way to make sure that you don't need to hard wire paths all over the place is to utilize one of the PHP magic constants, and then create a relative path to include files.

The best way to do this, is to establish a base variable in a known location, and then to include that file in any script that needs to use it.  This can be problematic, depending on the way you have structured your project, but the general idea is to establish this basic variable using the __DIR__ magic constant.  

Here is a pretty good blog post covering the functions available to implement these techniques:  https://www.gavsblog.com/blog/move-up-directory-levels-relative-to-the-current-file-in-php

So let's assume the path is:

/var/www/your_project/config

In this directory you create a script named path.php

<?php
$baseDir = realpath(__DIR__ . '/..');

The important thing to understand is that $baseDir will be /var/www/your_project

It does not matter the location of the file that includes the path.php script, $basedir will be the filesystem path.

So the only thing that is important to build off the $baseDir is to include it relative to the script that needs it.  At that point you can include all other paths, so long as you know them relative to your_project.

So let's say that you have a bunch of scripts in a directory named "utility" one named file.php.  The real path of this would be:

/var/www/your_project/utility/file.php

Assume that I have a script that needs to include a function defined as  "function myFile($path)".  

We'll also assume that you have some files stored outside the webroot that you want to deliver via PHP.  One in particular will be at the actual real path of:

/var/www/your_project/files/pdf/info.php

Assum you have an index.php file that is in a project subdirectory:

/var/www/your_project/admin/index.php

<?php
// your_project/admin/index.php

// this gets the $basePath variable
require_once('../config/path.php');

// Now we need access to utility/file.php
require_once($basePath . '/utility/file.php');

$result = myFile($basePath . '/files/pdf/info.pdf');

 

What this all does is insure that you never have to configure or hard code paths into variables or within your scripts using relative paths where you have to.  For the most part, the major frameworks use techniques like these, along with front controllers to insure that you never have to hard code a specific directory path structure into your application.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.