1

When I try

realpath('./')

I get "D:\Projects\Tickle\public"

But when I try

realpath('./models'); // or './models/'

I get "" or null. why is that? The strange thing is that when I try

realpath('../application/models');

I get "D:\Projects\Tickle\application\models" which is right

UPDATE

My directory structure looks like below (I am using Zend Framework)

/application
    /models
    /controllers
    /views
    /configs
    bootstrap.php  <-- this is where I am
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

2 Answers2

3

According to the output you posted, your directory structure seems to look like this:

D:\Projects\Tickle
    public
        foo.php
    application
        models

We are in foo.php. By using realpath('./models'); you refer to D:\Projects\Tickle\public\models which does not exist.

UPDATE If you want to get a file (relative to the current file), you should use something like this:

realpath(__DIR__ . '/models');

Or even better (__FILE__ is the current file you are in):

realpath(dirname(__FILE__) . '/models')

Using the Zend Framework, the APPLICATION_PATH constant is defined as realpath(dirname(__FILE__) . '/../application') (in public/index.php).

Nedec
  • 836
  • 10
  • 15
  • I missed the part where `realpath('./')` is public! but why? The code is actually in `/application/bootstrap.php` if you are familiar with Zend Framework. Isit because the code starts running from /public/index.php which calls bootstrap.php? using `realpath(__DIR__ . '/models')` works tho. Is `__DIR__` not same as `./`? – Jiew Meng Dec 16 '10 at 14:31
  • 1
    @jiewmeng You are in public since the file you call in the browser is the `public/index.php`. The bootstrap is just included in `public/index.php`, you are right. The difference between your two approaches is: `__DIR__` returns the directory of the current **file**. (It is a constant - different in each included file!) When you use `realpath('./')` you do not use the constant, the script takes `public/index.php` (called by the user) as starting point. Working with Zend, you should *always* use the constant `APPLICATION_PATH`, your models directory would be: `APPLICATION_PATH . '/models'`. – Nedec Dec 16 '10 at 16:44
2

Try following and see if you get false by any chance:

var_dump(realpath('./models'));

If I'm not mistaken, your tree structure is as shown bellow and the file you have above code in is public, correct? In that case, you are actually trying to get a real path of public/models.

public
    somefile.php
application
    models
David Kuridža
  • 7,026
  • 5
  • 26
  • 25
  • Yes I get false. Oh and I missed the part where `realpath('./')` is public! but why? The code is actually in `/application/bootstrap.php` if you are familiar with Zend Framework. Isit because the code starts running from /public/index.php which calls bootstrap.php? using `realpath(__DIR__ . '/models')` works tho. Is `__DIR__` not same as `./`? – Jiew Meng Dec 16 '10 at 14:29