mehulkar
mehulkar

Reputation: 4964

XAMPP Local installation File Path problems

I like to work on websites locally before uploading to my host. I use PHP/MYSQL servers in an XAMPP install.

I have multiple directories in XAMPP htdocs directory (one for each project). Each project usually has at least:

  1. header.php
  2. index.php
  3. footer.php
  4. styles/stylesheet.css

This worked fine until recently.

I am now working on a more extensive file/directory structure. Now, when /about/index.php calls header.php, the path to the stylesheet directory doesn't point in the right direction. Image paths no longer point in the right place either since they are all relative paths.

I tried pointing everything to the home directory first using a "/" at the beginning of every path, but in XAMPP the home directory now refers to localhost, instead of the directory for the particular project.

What is the solution? Is there a better way to be working on projects locally so I can upload to my web host simply, using all relative paths and not having to change them for live and dev versions of the website?

Upvotes: 0

Views: 9818

Answers (5)

bubencode
bubencode

Reputation: 77

The simplest solution as answered by @Vladimir Dimitrov in this thread goes as following (I will just copy it here):

The easiest way is to create separate virtual host for each site folder in /htdocs So you will access the http:// mysite.local instead of http:// localhost/mysite

There are two things to do: 1. edit C:\xampp\apache\conf\extra\httpd-vhosts.conf (by default) adding something like:

<VirtualHost *:80>
ServerName      mysite.local
DocumentRoot    C:/XAMPP/htdocs/mysite
</VirtualHost>
  1. edit c:\windows\system32\drivers\etc\hosts adding

    127.0.0.1 mysite.local

restart xampp and try http://mysite.local

Upvotes: 2

Tieson T.
Tieson T.

Reputation: 21191

I solve this problem by defining some constants:

#   index.php
#    handles pretty much everything for the site
define( 'DS', DIRECTORY_SEPARATOR );
define( 'ROOT', dirname(__file__) );
define( 'HOST', 'http://' . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] ) );

Then, for includes, I do something like this:

include ROOT . DS . 'directory' . DS . 'file_i_want.php';

For CSS and whatnot, it may be easier to just set the base URL in the markup.

Upvotes: 0

MrJ
MrJ

Reputation: 1938

Try including them using `DOCUMENT_ROOT for your PHP files, ie:

include($_SERVER['DOCUMENT_ROOT']."folder/header.php");

This assumes, when looking in the browser, header.php can be ound by going http://127.0.0.1/folder/header.php

For other files, such as CSS, Javascript you could define the location as follows:

define("SCRIPTS_URL", "http://127.0.0.1/_scripts/");

Include the above in your header.php file, and make sure you include header.php before calling the actual html header, eg:

<?php
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
?>
<html>
    <link rel="stylesheet" type="text/css" href="<?php echo SCRIPTS_URL; ?>stylesheet.css">
    ... etc etc ...

You can further combine define and build up directory parts, for example:

$project = "project_x";
include($_SERVER['DOCUMENT_ROOT'].$project."/header.php");
define("SCRIPTS_URL", "http://127.0.0.1/".$project."/_scripts/");

If you do it like above, then you only need to change the project variable, if you see...

Update

The below would be index.php:

<?php
// Make the header relative to index.php (as we don't know the project) - assume header is located at /_template/header.php and this file is located at /index.php [if, in future you have /content/index.php - then the below would be ../_template/header.php, etc]
if(file_exists("_template/header.php")){
    include_once("_template/header.php");
} else {
    die('Fatal error - no header found');
}

?>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>styles/stylesheet.css">
</head>
<body>
// Content goes here
</body>
</html>
<?php

if(file_exists(ROOTPATH."_template/footer.php")){
    include_once(ROOTPATH."_template/footer.php");
}
?>

And header.php:

<?php
define("PROJECT_NAME", "project_x");
define("ROOTPATH", $_SERVER['DOCUMENT_ROOT'].PROJECT_NAME."/");
define("BASE_URL", "http://".$_SERVER['SERVER_NAME']."/".PROJECT_NAME."/"); // $_SERVER['SERVER_NAME'] automatically puts 'localhost' or the domain name in automatically
?>

As you can see - everything is defined in this header file and when it is included on index.php - index.php can access those definitions, as can any other file that is included after the definition has been made (note that you cannot overwrite a definition and cannot define the same definition twice.

Upvotes: 0

Stelian Matei
Stelian Matei

Reputation: 11623

You could try this:

  • create a common configuration file
  • define a BASE_URL constant to your home directory (e.g. http://localhost/my_project/)
  • in your templates use all your links and references with BASE_URL

When you will deploy, you will need to change only one file.

You could also set a BASE_PATH constant to your directory (e.g. c:/xampp/htdocs/my_project). This might be useful when trying to include scripts from various sub-directories, without "guessing" the local path. (e.g. include BASE_PATH . 'templates/my_template.php')

Upvotes: 0

Maarten Kesselaers
Maarten Kesselaers

Reputation: 1251

Possibly this helps you? You have to edit some config to reference each site to a base-link. creating-multiple-sites-on-a-local-web-server

Upvotes: 0

Related Questions