dougou5844 2012-03-25 00:44
浏览 55
已采纳

定义文件目录; XAMPP

I'm running XAMPP for Mac.

When I first started working with PHP, I would define my directory with a variable:

 $root = "http://localhost/~aaron/SiteFolder/"; // Dev
 $root = "http://www.sitename.com/";            // Live

This will work, but it's a huge annoyance having to comment one out, depending on if I'm editing the dev site or the live site.

I'm slowly transitioning over to OOP habits and want to maximize efficiency if at all possible. So I'm attempting to define ROOT with the current directory as shown here:

define("ROOT", dirname( __FILE__ ) . DIRECTORY_SEPARATOR);

This outputs:

/Users/aaron/Sites/SiteFolder/

This is not entirely useful to me when I'm working on the dev site, because I want to be able to use ROOT as a prefix for my images, files, etc.

This obviously won't work:

<img src="/Users/aaron/Sites/SiteFolder/images/charizard.png">

What do I need to do to fix this problem to properly define my directory?

  • 写回答

3条回答 默认 最新

  • douhao2011 2012-03-25 01:11
    关注

    Based on your comments in my other answer, this is what you could do:

    You could create a simple installation page and ask the user for the url where they plan to host your web app.

    Then you can set this variable in a database or something.

    A lot of web apps do this: Wordpress, Joomla come to mind.

    EDIT:

    You could also try to build the url using the php $_SERVER variable. You can get the host, port, to build the url.

    Unfortunately this could be a problem if someone decides to say host your web app in something other than the root of the domain of subdomain.

    Something like 'http://domain/yourwebapphere/'

    You wouldn't be able to detect that without the user specifying it. If you used a single endpoint and used an htaccess for redirecting urls, you'd still need to specify a base url in the htacess for it to redirect properly.

    EDIT2:

    Ok, if you really want to create the url dynamically, here's some psuedo code on how to do it.

    $protocol = parse_server_protocol($_SERVER['server_protocol']);
    $port = $_SERVER['port']
    $host = $_SERVER['server_name'];
    //For use case where the app is not hosted at root of domain ("http://domain.com/test") for example
    $urlroot = parse_root($_SERVER["REQUEST_URI"]);
    
    $siteurl = buildRootUrl($protocol, $port, $host, $urlRoot);
    
    //Now save that root url in the database.
    

    If you are using htacess to create clean urls, your app would need to know how to write the baseurl in the instance where it is not hosted at the root of the domain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不同模型怎么用同一个shader
  • ¥15 安卓启动没有ais proxy与v4l2的log打印
  • ¥15 go怎么读取mdb文件里面的数据
  • ¥60 Matlab联合CRUISE仿真编译dll文件报错
  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?