开发者

php realpath not producing usable outcome

开发者 https://www.devze.com 2023-01-06 16:26 出处:网络
<?php $server = $_SERVER[\"SERVER_NAME\"]; $pathpath = realpath(\"../../files/uploaded_file.jpg\"); echo \"You can link to the file using the following link... $server$pathpath\";
<?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 fol开发者_JS百科der 'files' will always be in the same directory.


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"; 
0

精彩评论

暂无评论...
验证码 换一张
取 消