简体   繁体   中英

how to get the right url of file uploded to a local folder from localhost on Mac OS

I have apache+PHP+mySQL on the local Mac OS

and I just uploaded a file to the localpath folder, this is in uploaded.php and I just echo back the path so that the original php file can use it

 $targetPath = $_SERVER['DOCUMENT_ROOT'] ;
 echo '{"url":"'.$targetPath.'"}';

But it turns out what's in the url is /Library/WebServer/Documents , not what I have expected it as something like http://localhost/~username/ , because this is the url of a picture, I need to retrieve it using in the src , I did not how to do it.

for example: I just upload a file called 7.jpg, and what echo back is

/Library/WebServer/Documents/7.jpg

but I need something like http://localhost/~username/7.jpg

Thank you

$targetPath = 'http://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];

Here's why:

  • $_SERVER[HTTP_HOST] will output 'localhost' (or example.com, etc)
  • $_SERVER[REQUEST_URI] will output '/~username' (or /path/to/current/file)

See the PHP $_SERVER variable for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM