1
<?php
    $server = $_SERVER["SERVER_NAME"];
    $pathpath = realpath("../../files/uploaded_file.jpg");
    echo "You can link to the file using the following link... $server$pathpath";
?>

Unfortunately this produces the following...

www.example.com/home/fhlinux123/g/example.com/user/htdocs/ninja/base/files/1.doc

Whereas what I am after is as follows...

www.example.com/files/uploaded_file.jpg

I can't assume that the folder 'files' will always be in the same directory.

Andy Cheeseman
  • 283
  • 1
  • 3
  • 13
  • realpath resolves `..` from the current directory. Unless otherwise changed with `chdir`, that directory is the directory your PHP file is in... or called from in the case of a symlinked php file. – Powerlord Jul 12 '10 at 13:42

1 Answers1

2

That's because realpath returns the absolute path from the server box root, not the webserver htdocs root. You can retrieve the webserver htdocs root from $_SERVER["DOCUMENT_ROOT"], then strip that from the beginning of the result returned by realpath

Quick and dirty example:

$server = $_SERVER["SERVER_NAME"]; 
$pathpath = realpath("../../files/uploaded_file.jpg"); 
$serverPath = $_SERVER["DOCUMENT_ROOT"];
$pathpath = substr($pathpath,strlen($serverPath));

echo "You can link to the file using the following link... $server$pathpath"; 
Mark Baker
  • 209,507
  • 32
  • 346
  • 385