簡體   English   中英

如何在Mac OS上從localhost將文件的正確url升級到本地文件夾

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

我在本地Mac OS上有apache + PHP + mySQL

我只是將文件上傳到localpath文件夾中,該文件位於upload.php中,我只是回顯路徑,以便原始php文件可以使用它

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

但是事實證明, url/Library/WebServer/Documents ,而不是我所期望的http://localhost/~username/ ,因為這是圖片的url,我需要使用在src ,我沒有怎么做。

例如:我剛剛上傳了一個名為7.jpg的文件,回顯的是

/Library/WebServer/Documents/7.jpg

但是我需要像http://localhost/~username/7.jpg這樣的東西

謝謝

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

原因如下:

  • $_SERVER[HTTP_HOST]將輸出“ localhost”(或example.com等)
  • $_SERVER[REQUEST_URI]將輸出“ /〜用戶名”(或/ path / to / current / file)

有關更多信息,請參見PHP $ _SERVER變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM