[英]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.