[英]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 我在本地Mac OS上有apache + PHP + mySQL
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 我只是将文件上传到localpath文件夹中,该文件位于upload.php中,我只是回显路径,以便原始php文件可以使用它
$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. 但是事实证明,
url
是/Library/WebServer/Documents
,而不是我所期望的http://localhost/~username/
,因为这是图片的url,我需要使用在src
,我没有怎么做。
for example: I just upload a file called 7.jpg, and what echo back is 例如:我刚刚上传了一个名为7.jpg的文件,回显的是
/Library/WebServer/Documents/7.jpg
but I need something like http://localhost/~username/7.jpg
但是我需要像
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[HTTP_HOST]
将输出“ localhost”(或example.com等) $_SERVER[REQUEST_URI]
will output '/~username' (or /path/to/current/file) $_SERVER[REQUEST_URI]
将输出“ /〜用户名”(或/ path / to / current / file) See the PHP $_SERVER variable for more info. 有关更多信息,请参见PHP $ _SERVER变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.