Php – site root relative path confusion in php

directoryPHPrelative-path

I'm trying to upload images to server, create a thumbnail from the uploaded image, and save the relative paths to mysql database . . . . The image path is

     /mysite/images/

from my site root (which is wamp/www/mysite/images in my local testing environment)

Now, I'm performing a directory check with

   if(!is_dir($path) && !is_writable($path)){
    throw new Exception ("$path is not valid .. ");
    }

where path is /mysite/images/ , thinking that initial '/' will denote the root.

But, the script was throwing an exception saying invalid path name, and my

     move_uploaded_file($tmp_name, $path.$filename)

too was not working.

PS: the path mysite/images/ also throws an error and the file is not moved

Now, I have worked around by appending document root manually to path name for both directory checks and move file , which is now looking like

      if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path) && !is_writable($_SERVER['DOCUMENT_ROOT'].$path)){
    throw new Exception ("$path is not valid .. ");
    }

      move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'].$path.$filename)

I also changed the path to

    mysite/images/ 

removing the initial slash intended for site root. This is working perfectly.

But, I cannot understand why directory check and move uploaded file snippets are not taking site root relative links.

Any ideas is appreciated.

Best Answer

The problem is with the path representation.

In linux /mysite will try to access the directory in the root level of file system. Windows this might not create an error. for is_file() kind of API's it should the absolute path and not relative ones.