0

Here is my directory listing :

>htdocs
-->MyWeb
---->admin
------>cms.php
---->images
---->controller
------>post.php
------>general.php
---->model
------>posts.php
------>sessions.php
------>connections.php
---->index.php

and here are the includes for each page starting from top :

cms.php:
    require_once('../model/sessions.php');
    require_once('../controller/post.php');
post.php:
    require_once('general.php');
    require_once('../model/posts.php');
general.php : none 
posts.php:
    require_once('sessions.php');
    require_once('connections.php');
sessions.php:
    require_once('connections.php');
connections.php : none
index.php:
    require_once('controller/post.php');

condition : tested everything fine with cms.php, i got access to all the function using forms provided by the controller, and the controller got the data from model. now, the administration things are done, time to get to the index.php to show the data, i put inside index.php, when i open it, here's what happened :

I got errors which tell me that the "require_once()" function cannot locate the specified file. I did this with cms.php and its all right. The errors are not from index.php, but from the file included within it (post.php).

lets take a look again in index.php, it has included the controller, post.php using "require_once('controller/post.php') ;", but the post controller itself, includes another file, which is the post model "require_once('../model/posts.php')". The problem occurs when index.php view the required file of the controller using top folder perspective (MyWeb), then reads the file included in post controller as exactly "require_once('../model/posts.php')" = up one folder of index.php, folder model, which mean :

htdocs>model>posts.php which is of course, didn't exist. I was really confused at the first time, then i create some cheats for this :

1. I put a variable $check = true ; before the require_once('controller/post.php') ; in index.php
2. then for each page that requires another file, i put these :
    //for example, in sessions.php
    if(isset($check)) {
        //old link which is like this : require_once('connections.php') ; has become :
        require_once('model/connections.php') ;
        //so in index.php perspective, the file can be reached
    }
    else {
        //old links, only executed when this page was not viewed from index.php
        require_once('connections.php') ;
    }
3. it works now, cms.php read the includes from old links, while index.php will read the included from new links.

Now, even though it works, my codes look ugly and inconsistent if in the future, i might add more files, more includes. question : do anyone has better solution for this ? how to access working directories of the application, such in linux using : ~/controller/post.php or ~/model/posts.php. so the address will be consistent. can't use getcwd(), i dont want to show the absolute path of my files. does anyone can give me better solution for specifying directories such in my case ?

note : pardon my english

2 Answers 2

1

how to access working directories of the application, such in linux using : ~/controller/post.php or ~/model/posts.php.

$_SERVER['DOCUMENT_ROOT']
Sign up to request clarification or add additional context in comments.

4 Comments

it shows C:/xampp/htdocs, what i want is something which not displaying absolute path. Thank you for your answer anyway.
@user absolute path is the only thing you need, despite of your strange fantasies.
Im new in php, absolute path will not give any security problem right ? and also, will this work when i run the server at home, and when i run it on some hosting places (dunno if they got some kind of server rules or policies). So i will use it like $url = $_SERVER['DOCUMENT_ROOT'] . '/MyWeb/controller/file.php' ; ?
@AzDesign DOCUMENT_ROOT is a common solution. You need absolute path anyway, as it's the only way to address any file from any folder.
0

DOCUMENT_ROOT works, but is useless if you need to be able to move your application around in subdirectories like

domain.com/apps/myapp
domain.com/apps/myapp/v1

in that case, my favourite approach is to define the root directory in a configuration file that gets called in every script:

// in /index.php
define("BASEDIR", dirname(__FILE__));

you can then use the BASEDIR constant wherever you need to substitute the full path to the application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.