tags:

views:

237

answers:

5

I develop my project at ~/Dropbox/db/. I need to move the folder continuously to /var/www to be able to see the result in Firefox because it is the only place where I have Apache.

My filetree is at ~/Dropbox/db/

.
|-- handlers
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php
|-- index.php
|-- forms
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php

I have for instance the following reference in the file ~/Dropbox/db/forms/login.php:

include '../handlers/login.php';

which is awful, since everything breaks after a while. For instance, I start Firefox at index.php. This means that all PATHS are relative to index.php. This makes the command above meaningless.

Perhaps, the only solution to the problem is to make all PATHs absolute in all my files. I have tried to avoid this because this would force me to use SED's replacement command.

I would like to have a similar command as LaTeX's \graphicspath for forms and handlers such that I can simply say where to find the given files in one file. I found out that PHP has the following function which needs to be at the beginning of index.php. However, it does not seem to be enough, since I cannot extend it to forms and handlers, for instance.

$path_parts = pathinfo('/var/www/index.php');

How can you have a LaTeX-like PATH -system for PHP?

+1  A: 

I don't know exactly what LaTeX does, however I do not know why the errors you describe are happening, or is it that you think this would happen? When you point firefox to index.php all paths in index.php will be relative to index.php however all paths in login.php will (should) be relative to login.php , even if login.php is included by index.php. Also one way of not having to do such things is to use a variable to store the base path. But that won't work well for the layout you have.

On the other hand I do have a solution to your problem of moving files around, just reconfigure apache for me the file needing edit was /etc/apache2/sites-enabled/000-default you will need to do

sudo gedit /etc/apache2/sites-enabled/000-default

and enter your password. change the paths to the full path to the Dropbox/db directory

it should end up something like this, replace USERNAME with your username.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/USERNAME/Dropbox/db
    <Directory />
     Options FollowSymLinks
     AllowOverride None
    </Directory>
    <Directory /home/USERNAME/Dropbox/db/>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride None
     Order deny,allow
     deny from all
     allow from 127.0.0.1
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
     AllowOverride None
     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
     Order allow,deny
     Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Then you wont have to move the files around.

**Which command should you run to see the update in the Apache -file? - What is the URL after the change in the file?** Is it `localhost/index.php` if we have index.php at the folder /home/masi/Dropbox/db/index.php`?
Masi
There was an error in my config above, specifically the line DocumentRoot /home/USERNAME/Dropbox/db I have edited it do check. (before it was DocumentRoot /home/USERNAMEDropbox/db) should be as it is now.the command is sudo /etc/init.d/apache2 restart or if that does not work then sudo /etc/init.d/apache2 stop followed by sudo /etc/init.d/apache2 startall the urls will remain the same and yes it will be localhost/index.php
+1  A: 

A good solution is to have your entire program run from one file, normally index.php. In that case what you would do is accept a GET parameter which tells you what action to do. This would make it so that you could include files always relative to index.php.

For instance: index.php?action=login or index.php?action=register.

This also makes for unique URLs so normal browsing behavior is retained.

Although this might seem a bit unusual at forst there are a number of PHP programs which operate in this way (e.g. Drupal or Symfony) and it works very well.

codeincarnate
Could you give an example how to use the GET -parameter, for example, to the file `handler_login.php`, please. I use POST -method in my forms at the moment. - **How can you achieve the same result with POST -method than with GET?**
Masi
+2  A: 

Approach 1

Is include_path what you're looking for? You can set it either in php.ini, or in your apache vhost:

<VirtualHost *:80>
    ...
    php_value include_path ".:/usr/share/php:/usr/share/pear:/usr/local/share/php"
    ...
</VirtualHost>

Not quite as nice as kpathsea (what Τεχ uses), but as close as you can get. I prefer this approach, as it makes it easy to keep your include files outside your document root (so you don't have to worry about someone executing them directly).

Approach 2

You can get your document root using $_SERVER['DOCUMENT_ROOT']. So you can use that to write a full include path.

derobert
Do you mean that I should have the `php_value include_path ".:/home/masi/Dropbox/db/:"` to be able to see my php -file in Firefox by `localhost/index.php` when I have index.php at `/home/masi/Dropbox/db/index.php`?
Masi
when you set include_path, you're giving PHP a list of places to look for include files. If you have it set to `/home/masi/Dropbox/db/`, and you `include("foo.php")`, then PHP will look for `/home/masi/Dropbox/db/foo.php`. If you have more than one directory set (`:`-separated), then it'll search each directory in order.
derobert
+2  A: 

Another approach would be to create a symbolink link to /var/www/ from ~/Dropbox/db/ (Don't forget to set htaccesss to follow symlinks)

+4  A: 

If you always want your includes to be relative to the folder the current executing file is currently residing in, you can use the following:

include(dirname(__FILE__) . '/../hello.php');

So, if that code is in x.php and you include x.php from y.php, it doesn't matter in which directory y.php is since the include is always relative to the directory x.php is in.

I personally think it's the most effort-less way of achieving exactly what you want. You don't need any special configuration or setup, and all paths are "relative".

From the PHP Documentation

__FILE__: The full path and filename of the file. If used inside an include, the fill path and filename of the included file is returned.

Andrew Moore