douyan3478 2016-11-30 10:15
浏览 22
已采纳

用php获取当前目录的路径?

I'd like to get the correct php code to display filepath to a folder on my server. Currently I put the actual filepath in my script which looks like this: /home/username/mydomain.com/dir/upload/

So the script is installed in a subfolder "dir", and I need the path to the "upload" folder

I used this, but it doesn't seem to work, because it seems to put uploaded files into the root:

$_SERVER["DOCUMENT_ROOT"] . '/upload' 
  • 写回答

1条回答 默认 最新

  • doufan1899 2016-11-30 10:17
    关注

    There is a __DIR__ constant which will contain the script file's directory path.

    echo __DIR__;

    So to get your upload path you could do:

    echo __DIR__ . '/upload/';

    For PHP < 5.3 you can do

    echo dirname(__FILE__);

    See http://www.php.net/manual/en/language.constants.predefined.php for other 'Magic Constants'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件