Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2013
    Location
    Calgary AB,Canada
    Posts
    55
    Thanks
    28
    Thanked 1 Time in 1 Post

    How to get the real physical path to load files.

    Hi,

    I tried to upload a file into the server by the following command in the PHP:


    PHP Code:
    $moved move_uploaded_file($_FILES["file"]["tmp_name"], 
       
    "/httpdocs/images/test/" $_FILES["file"]["name"]); 

    It did not work, because, the destination path I mentioned above "/httpdocs/images/test/" is wrong. I figured it out by inserting the following code at the beginning of the PHP code to troubleshoot:

    PHP Code:
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    $moved move_uploaded_file($_FILES["file"]["tmp_name"], 
       
    "/httpdocs/images/test/" $_FILES["file"]["name"]); 
    The above code gave this error:

    Warning: move_uploaded_file(/httpdocs/images/test/logo-bkp.png): failed to open stream: No such file or directory in /var/www/vhosts/mywebsite.ca/httpdocs/UploadImg03.php on line 19

    I need to find out how to extract the path ("/var/www/vhosts/mywebsite.ca/") without hard coding it ?

    Thanks a lot,

    Isaac

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Depends on what you are trying to do. Note that /var/www/vhosts/mywebsite.ca will only apply on a nix machine; the previous thread you had used H:\ which is a windows machine.

    You can get the physical path to *this* file by using __FILE__ for the entire file, or with newer versions of PHP just the directory with __DIR__. I would suggest making use of these and working relative to the executing script instead of ever concerning yourself with an absolute filepath.
    So from that particular file located at /var/www/vhosts/mywebsite.ca/httpdocs/UploadImg03.php, to get to the path "/var/www/vhosts/mywebsite.ca" you want it would be:
    PHP Code:
    $sPath __DIR__ '/../'
    Which would then be a relative path landing on /var/www/vhosts/mywebsite.ca/. If you want to resolve it (although rarely necessary), you can do so using realpath() function.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Isaac Sureshkum (10-16-2013)

  • #3
    New Coder
    Join Date
    Jun 2013
    Location
    The Republic of Texas
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    PHP Code:
    echo $_SERVER['DOCUMENT_ROOT']; 

  • Users who have thanked AbraCadaver for this post:

    Isaac Sureshkum (10-16-2013)

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Quote Originally Posted by AbraCadaver View Post
    PHP Code:
    echo $_SERVER['DOCUMENT_ROOT']; 
    That is what you need to know.

    On 'nix and Apache, / is document root, i.e. the directory of the server, ./ is the current working directory, the one the current page is in, ../ is one directory up from the one the page is in. So

    ../Images/ is up one directory and into the Images directory, or

    /Images/ is into the Images directory which is located in the server root.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • Users who have thanked DrDOS for this post:

    Isaac Sureshkum (10-16-2013)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    $_SERVER['DOCUMENT_ROOT'] is also a server directive. That means there is no guarantee that it will ever exist, so keep that in mind if you rely on it. EG: it'll never be available if you run PHP on the cron.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Isaac Sureshkum (10-16-2013)

  • #6
    New Coder
    Join Date
    Jun 2013
    Location
    Calgary AB,Canada
    Posts
    55
    Thanks
    28
    Thanked 1 Time in 1 Post
    Thanks Fou-Lu for your nice explanation. thanks a lot and really appreciated.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •