如何获得正确的url文件上传到本地文件夹从localhost在Mac OS


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]将输出'/~username'(或/path/to/current/file)