Search code examples
phppathwampwampserverabsolute-path

php absolute path not working on local computer


I'm working with an existing PHP web application. It's running fine on the remote Linux server but I can't run it on WAMP server, Windows 7 on my local computer. The problem is the previous developer's used absolute path all over the code. for example one of them is:

require '/disk2/html/src/web_tool/db_connect.php';

my localhost folder is F:\wamp\www and I have the same structure: F:\wamp\www\myProject\disk2\html\src\web_tool\db_connect.php

But it can't find the db_connect.php. I can't change the path because it's a big project and if I want to, I have to find and change more than 400 line of code. I'm not sure what the problem is and how to make it work. Any suggestion is highly appreciated.

EDIT: I ran a simple test on my WAMP Server. I created this file structure: F:\wamp\www\test\myfolder\index.php with this code:

require '/myinc.php';

and F:\wamp\www\test\myinc.php with this code:

echo 'It works!';

I get this warning:

Warning: require(/myinc.php): failed to open stream: No such file or directory in F:\wamp\www\test\myfolder\index.php on line 2

and this error:

Fatal error: require(): Failed opening required '/myinc.php' (include_path='.;C:\php\pear') in F:\wamp\www\test\myfolder\index.php on line 2

I don't have such a folder as C:\php\pear on my disk.

The WAMP server is just installed, haven't touched anything in the php.ini or other config files. the version is:

WAMP SERVER 64bit version 2.5 including:

Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMyAdmin : 4.1.14 SqlBuddy : 1.3.3 XDebug : 2.2.5


Solution

  • I think your best solution would be to move the site source and create a Virtual Host, then the absolute paths would work and you can address the site properly as well.

    First move the disk2 folder and all its subfolders to the root of your F: drive.

    F:\disk2.....
    

    Then create a Virtual Host to describe its new location.

    Edit \wamp\bin\apache\apache2.x.y\conf\extra\httpd-vhosts.conf

    If this is your first Virtual Host definition, remove the existing contents of this file.

    I have assumed that F:\disk2\html is the correct folder to become your DocumentRoot.

    Add this to the file

    <IfDefine !APACHE24>
        NameVirtualHost *:80
    </IfDefine>
    
    ## so the the wamp menu page loads
    <VirtualHost *:80>
        DocumentRoot "F:/wamp/www"
        ServerName localhost
        ServerAlias localhost
        <Directory  "F:/wamp/www">
            AllowOverride All
    
            <IfDefine APACHE24>
                Require local
            </IfDefine>
    
            <IfDefine !APACHE24>
                Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1 
            </IfDefine>
    
        </Directory>
    </VirtualHost>
    
    
    <VirtualHost *:80>
        DocumentRoot "F:/disk2/html"
        ServerName example.dev
        ServerAlias www.example.dev
    
        <Directory  "F:/disk2/html">
            AllowOverride All
    
            <IfDefine APACHE24>
                Require local
            </IfDefine>
    
            <IfDefine !APACHE24>
                Order Deny,Allow
                Deny from all
                Allow from 127.0.0.1 localhost ::1 
            </IfDefine>
    
        </Directory>
    </VirtualHost>
    

    Save this file.

    Edit httpd.conf and remove the comment from this line

    #Include conf/extra/httpd-vhosts.conf
    

    remove the # in col 1 and save that file.

    Now edit the hosts file C:\windows\system32\drivers\etc\hosts and add these lines.

    127.0.0.1  example.dev
    ::1 example.dev
    

    This is a windows protected file so you will have to open your editor using the "Run as Administrator" or you wont be allowed to save your changes.

    Now start a command windows also using the "Run as Administrator" and execute these 2 statements to refresh the DNS cache with your changes to the hosts file.

    net stop dnscache
    net start dnscache
    

    Now restart Apache so that it picks up your configuration changes, and you should be able to use this address in the browsers address bar.

    example.dev
    

    The DocumentRoot should be as it was on the live server and the absolute paths used in the PHP code should also now work as they do on the live server.

    Change example.dev to whatever makes sense as related to your live sites domain name.