PHP全局路径设置

发布于 2024-11-27 06:10:46 字数 824 浏览 1 评论 0原文

我有这个设置。

 root dir| 
       index.php
       config.php
       file.php  |
                 |
                 |scripts|a.js
                 |
                 |account
                         |index.php
                         |         |
                         |member   |index.php
  • 现在,我已将成员目录的index.php 包含到帐户目录的index.php 中。另外,帐户index.php 包括config.php,其中包含,

    define( '路径', (__DIR__) );
    

现在,对于我使用的帐户index.php 中的所有内容,

require_once( PATH . '\file.php' ); 

并且工作正常。但是,当我尝试添加脚本 src 的路径时,例如,

<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>

我收到错误,那么如何使用全局定义的路径将脚本文件夹中的 a.js 包含到帐户的 index.php 中。

谢谢。

I have this setting.

 root dir| 
       index.php
       config.php
       file.php  |
                 |
                 |scripts|a.js
                 |
                 |account
                         |index.php
                         |         |
                         |member   |index.php
  • Now, I've included index.php of member dir into index.php of account dir. Also , the account index.php includes the config.php which contains,

    define( 'PATH', (__DIR__) );
    

Now , for all includes in account index.php I use,

require_once( PATH . '\file.php' ); 

and is working properly. But when I try to add the path for script src such as,

<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>

I get an error, so how can i include the a.js in scripts folder into index.php of account using the globally defined path.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-12-04 06:10:46

PHP“__DIR__”和“__FILE__”对于服务器来说是绝对的。您不需要在脚本中使用其中任何一个。

<script src="/scripts/livevalidation.js"></script>

另外,你的 PHP 看起来有一些语法错误,这是正确的(尽管仍然不起作用:

<script src="<?php echo PATH.'/scripts/livevalidation.js'; ?>"></script>

The PHP "__DIR__" and "__FILE__" are absolute to the server. You shouldn't need to use either for your script.

<script src="/scripts/livevalidation.js"></script>

Also, your PHP looks like it has some syntax errors, this would be correct (although still wouldn't work:

<script src="<?php echo PATH.'/scripts/livevalidation.js'; ?>"></script>
等数载,海棠开 2024-12-04 06:10:46

您的 script 标记的 PHP 语句中缺少 printecho 语句。您还将串联句点放置在错误的位置。然而,最重要的是,您尝试包含的 JavaScript 不需要位于 PHP 语句中。

话虽如此,最后一行应如下所示:

<script type="text/javascript" src="<?php echo PATH ?>scripts/livevalidation.js"></script>

然而,最重要的是,我认为上面的内容不会按您的预期工作。 __DIR__ 输出服务器端文件系统路径,这在通过 HTTP 导入 JavaScript 时没有意义。我建议更多的内容如下:

<?php define('URL_ROOT', '/'); ?>

<script type="text/javascript" src="<?php echo URL_ROOT ?>scripts/livevalidation.js"></script>

在上面的示例中,URL_ROOT 将指向一个绝对 URL,在该绝对 URL 下提供静态媒体(CSS、JavaScript 等)。

You're missing a print or echo statement in the PHP statement in your script tag. You're also placing the concatination periods in the wrong place. On top of that, however, the JavaScript you're trying to include doesn't need to be in the PHP statement.

All that said, the final line should read like the following:

<script type="text/javascript" src="<?php echo PATH ?>scripts/livevalidation.js"></script>

On top of that, however, I don't think the above will work as you expect. __DIR__ outputs a server-side filesystem path, which wouldn't make sense when importing JavaScript over HTTP. I'd recommend something more along the lines of the following:

<?php define('URL_ROOT', '/'); ?>

<script type="text/javascript" src="<?php echo URL_ROOT ?>scripts/livevalidation.js"></script>

In the above example URL_ROOT would point an absolute URL under which your static media (CSS, JavaScript, and so on) is served.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文