1

I would like to know if this kind of setup is possible as I'm having a lot of trouble setting it up. I am trying to set up a website with a file structure like this:

  • project1 (folder)
    • index.php
    • style.css
    • include.php
      • admin (folder)
        • index.php

The parent "project1" folder is located on 'server1' in D:\Projects\project1.

The website is accessed via virtual directory "http://server1/project1".

I would like to be able to write all paths for includes, images srcs, stylesheets etc, in the format '/style.css' so that the files can be found regardless of where they are required. E.g. the following link should work whether it's placed in the 'project1' folder or the 'admin' folder:

<link rel="stylesheet" type="text/css" href="/style.css">

EDIT: as mellamokb suggested, using href="/project1/style.css" seems to work from both 'index.php' files. However I can't seem to find a similar solution for the PHP INCLUDE.

If I use this...

<?php include_once '/include.php'; ?>

...in both index.php files, the one in the admin directory cannot find the file.

I expected $_SERVER['DOCUMENT_ROOT'] to return "D:\Projects\project1" but it returns D:\staging and I can't work out why. I've tried a few different methods of setting the project 'root' but I've just ended up confusing myself even more! The virtual root of the project should be 'http://server1/project1' and the physical root should be D:\Projects\project1, but I can't work out how to force this.

Community
  • 1
  • 1
valoukh
  • 541
  • 1
  • 8
  • 19
  • What is the virtual directory path to `admin`? The root-relative path `/style.css` should work if admin is a virtual subdirectory of `http://server/`. – mellamokb Jan 08 '13 at 16:56
  • 'admin' is just a subfolder - do I need to set this up as a separate virtual dir? – valoukh Jan 08 '13 at 16:59
  • If you navigate to `http://server1/project1/admin/index.php`, a reference to `/style.css` will be resolved by the browser to `http://server1/style.css`, which appears to be correct. Did you try this and have a problem? – mellamokb Jan 08 '13 at 16:59
  • Yes, if I navigate to `http://server1/project1/admin/index.php`, firefox returns the error -- `GET http://server1/style.css [HTTP/1.1 404 Not Found 0ms]` The error is correct as 'style.css' is located in `http://server1/project1`, not `http://server1/` – valoukh Jan 08 '13 at 17:07
  • Oh. Then use `/project1/style.css`, or put the resource files in a common virtual folder, like `/resources/style.css`. – mellamokb Jan 08 '13 at 18:14
  • I think I'm confusing matters a bit here; this works perfectly for stylesheets and http links, but if I'm not mistaken the PHP INCLUDE function uses local paths? So using `` works from `http://server1/project1/index.php` but not from `'http://server1/project1/admin/index.php'` – valoukh Jan 09 '13 at 08:23
  • that will surely not work. They are not in the same directory. Read about navigating through directories. check http://forums.htmlhelp.com/index.php?showtopic=2922 – IROEGBU Jan 09 '13 at 12:26
  • please see: http://php.net/language.constants.predefined - you can include relative to any file with the `__DIR__` constant. That is independent to the webserver or operating system. – hakre Mar 30 '13 at 10:21

1 Answers1

1

You could create a file that contains constants and have something thus define('BASE_URL', "http://server1/project1/"). Then you can work relatively using the constant variable BASE_URL thus,

<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>style.css">

You can also handle other files by attaching the 'relative' address.

include (BASE_URL . '<file you want to add>');
IROEGBU
  • 948
  • 16
  • 33
  • This works well, however I think I'm asking the wrong questions. Your method solves my issue when linking stylesheets etc, but when I try to include a file via PHP INCLUDE, I am struggling to find a universal way of making sure it can find the file – valoukh Jan 09 '13 at 08:27
  • In addition I have the same problem with stylesheets, e.g. background: url('/Images/background.png') cannot benefit from the constant method :( – valoukh Jan 09 '13 at 08:58
  • ok... I see the problem. Do structure your folders such that related resources are close to each other. If 'Images' folder is in the same directory as style.css, just do background: url('Images/background.png') – IROEGBU Jan 09 '13 at 12:20