Path to folder

Hello. I have a file login that redirects to the login page if the user enters the correct details.

header('Location:./employers/welcome_member.php');

The location of the file is in the folder “employers” how can specify the path to that folder?

Thanks

Try adding the following before the header statement and select your relative destination folder

echo getcwd(); die;

@John_Betong I tryed this:

$path = $_SERVER['DOCUMENT_ROOT'];

and then:

header('Location:'.$path.'./employers/welcome_employer.php');

echo $path displays: /Applications/MAMP/htdocs/mysqli
welcome_employer.php is in …mysqli/employers

1 Like

getcwd); displays the Current Working Directory and you could set your relative path to rhe result.

$absolutePath = $_SERVER['DOCUMENT_ROOT'] .'/employers/welcome_employer.php';

header('Location:.'  .$absolutePath );

There is a problem with the second Absolute Path solution. there is a good chance the path will be incorrect when uploaded to your server :frowning:

To alleviate this problem I have a config.php file that tests for your Localhost and sets Path Constants:

config.php


define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );

// best to set paths with a trailing slash
if( LOCALHOST ) {
  define('PATH_EMPLOYERS',  $_SERVER['DOCUMENT_ROOT'] .'/employers/');
  define('PATH_IMAGES',   $_SERVER['DOCUMENT_ROOT'] .'/images/');
  define('
}else{
  define('PATH_EMPLOYERS',  $_SERVER['DOCUMENT_ROOT'] .'/online/employers/');
  define('PATH_IMAGES',  $_SERVER['DOCUMENT_ROOT'] .'/employers/images/');
}

// usage: - works for both Localhost and Online, no need to have two different files :

// WRONG  - please read  @RavenVelvet following post
header('Location: '  .PATH_EMPLOYERS  .'employer.php');

// CORRECT URI PATH 
header('Location: '   .'/employer.php');
or
header('Location: http://'   . $_SERVER '[HTTP_REFERER']   .'/employer.php');
// http://localhost/employer.php

There seems to be a bit of confusion here stemming from using filesystem paths.

The DOCUMENT_ROOT folder, as specified in your web server configuration, points to a certain location on the filesystem. That’s literally the root of your web site.

So, “/” when appended to the domain name means the web server should look in this DOCUMENT_ROOT folder for an appropriate index file (in our game, it’s usually called index.php).

The employers folder, if it’s located inside the DOCUMENT_ROOT folder, would be referenced by adding the folder name to the root path

So. “/” + “employers” = “/employers”

To get the web server to server the “welcome_employer.php” file inside the employers folder, which is inside the DOCUMENT ROOT folder, you would need a relative path of

“/employers/welcome_employer.php”

Note that the actual DOCUMENT_ROOT value isn’t in there at all - it’s represented by the first “/”

If you really wanted to supply the absolute web path to this file, you would provide the scheme, domain name and (optional) port as required.

So the absolute path locally might be

http://localhost/employers/welcome_employer.php

And on the production server, it might be

https://www.mywebsite.com/employers/welcome_employer.php

Generally, you’re better off just using the relative path, so your location header would read exactly like this in both local development and production environments

header("Location: /employers/welcome_employer.php");

That leading “/” anchors the request path resolution to being relative to the actual folder that the web server process understands to be the DOCUMENT_ROOT.

Is that anything like the info that you were looking for?

@RavenVelvet
Please accept my apologies for incorrect information.
I have amended my post to show the error.

Many years of PHP programming and I still confuse FILEPATHS with SERVER URIs :frowning:

The absolute file path I believe is sometimes necessary when searching for files or for changing file names, etc.

Example:

echo '$_SERVER["DOCUMENT_ROOT"] == ' .$_SERVER["DOCUMENT_ROOT"];  
  echo '<br />';

$fName = 'favicon.ico';
echo 'Filemame == '.$fName .'<br />';; 
  
// SUCCESS - Found the favicon.ico
   $ff = $_SERVER['DOCUMENT_ROOT'] .'/' .$fName;
   echo '<br /><b>Looking for File : </b> '. $ff .'<br />';
   if( file_exists($ff) ):
     echo '<b>FOUND IT : </b> ', $ff .'<br />';
  
     echo '<img src="/' .$fName .'" alt="#" /><br />';
     echo '<img src="http://localhost/' .$fName .'" alt="#" />';
     echo '<br /><br />';
   endif;

// FAILED TO FIND - favicon.ico 
   $ff = '/' .$fName;
   echo '<br /><b>Looking for File: </b> ', $ff;
   if( file_exists($ff) ):
     echo '<br /><b>FOUND IT : </b> ', $ff;

     echo '<img src="/' .$fName .'" alt="#" /><br />';
     echo '<img src="http://localhost/' .$fName .'" alt="#" />';
   endif;

Try the following to find the Server Settings:
echo '<pre>' .print_r($_SERVER, true) .'</pre>';

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.