引用网络资产时的绝对相对路径问题 - 需要帮助 - php 相关

发布于 2024-08-06 17:21:51 字数 428 浏览 2 评论 0原文

大家好,我现在有点麻烦了。首先,我用 PHP 构建了一个带有管理面板的简单 CMS,目录结构如下:

root/
    ->admin/
    ->images/

它工作得很好,但是客户需求发生了变化,他们希望不必将管理文件夹作为根目录中的文件夹进行访问它可以作为 Web 子域进行访问。所以 www.site.com/admin 变成了 admin.site.com

然而,这已经搞砸了,几乎破坏了我所做的所有引用。就像我在 CMS 上上传图像一样 - 但是现在在 ../images 上上传不起作用,因为它现在位于子域下,而且我也试图从那里相对引用图像,这让我一团糟。我几周来一直在尝试修改我的配置文件,但无法解决这个问题:( - 请帮忙 - 在前端,网站没问题,但我的管理部分一团糟:(

我正在使用PHP 和 MySQL。

Hi guys I'm in a bit of a pickle here now. Well to start with I built a simple CMS in PHP with an admin panel the directory structure is like this:

root/
    ->admin/
    ->images/

It worked fine as is however the client requirements changed and they wanted that instead of having to access the admin folder as a folder within the root it be accessed as a web subdomain. so www.site.com/admin becomes admin.site.com

However this has terrible messed up and destroyed practically all the referencing I had done. Like I upload images on the CMS - however now uploading on ../images doesn't work as its now under a subdomain and I'm all messed up in trying to relatively reference images from there too. I've been trying to hack away at my config file for weeks and can't get to fix this :( - help please - on the front end the site is o.k. but my admin section is all messed up :(

I'm using PHP and MySQL.

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

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

发布评论

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

评论(3

鱼忆七猫命九 2024-08-13 17:21:51

听起来您已经了解了相对路径的毒性有多大。

可能的快速修复:如果您复制/symlink/alias admin.domain.com/images 指向前端站点上的同一图像文件夹,会发生什么?我认为多余的“../”基本上会被忽略。

一般来说,更永久地不要使用相对路径。它们只会给你带来痛苦。有几种策略:

1) 定义一些常量,指向图像、CSS 等的正确位置:

define('IMG_DIR','/images');
define('CSS_DIR','/images');
// ... some time later
echo '<img src="' . IMG_DIR . '/myimage.jpg'"/>';

2) 更好:只需维护一个常量来告诉您应用程序所在的位置。

define('APP_ROOT','/myapp'); //could be chanted to just '/' if it doesn't live in some folder on the server
// ... later that day ...
echo '<img src=\"' . APP_ROOT . '/images/myimage.jpg"/>';
// ... or maybe you need to link to a logout script?
echo '<a href="'. APP_ROOT . '/auth/logout.php">Log Out</a>';

重要的是假设您的应用程序可能需要从根目录(“/”)或服务器上的某个目录等运行。

这同样适用于您可能纯粹在服务器端执行的任何文件系统操作。使用绝对文件系统路径。如果您的主应用程序有一个像“config/config.php”这样的脚本,您可以将其放在顶部:

define('APP_FS_ROOT',realpath(dirname(__FILE__) . '/..'));

Sounds like you've learned how toxic relative paths can be.

Possible quick fix: what happens if you copy/symlink/alias admin.domain.com/images to point at the same images folder that lives on your front-end site? I think that extra "../"es will basically be ignored.

More permanently, and in general, don't use relative paths. They will cause you nothing but pain. A couple of strategies:

1) Define some constant that points at the right location for images, css, etc:

define('IMG_DIR','/images');
define('CSS_DIR','/images');
// ... some time later
echo '<img src="' . IMG_DIR . '/myimage.jpg'"/>';

2) Much better: just maintain one constant that tells you where your application lives.

define('APP_ROOT','/myapp'); //could be chanted to just '/' if it doesn't live in some folder on the server
// ... later that day ...
echo '<img src=\"' . APP_ROOT . '/images/myimage.jpg"/>';
// ... or maybe you need to link to a logout script?
echo '<a href="'. APP_ROOT . '/auth/logout.php">Log Out</a>';

It's important to assume you application might need to run from the root ("/") or some directory on the server, etc.

The same goes for any filesystem operations you might do purely on the server side. Use absolute filesystem paths. If your main application has a script like "config/config.php", you could stick this at the top:

define('APP_FS_ROOT',realpath(dirname(__FILE__) . '/..'));
巷雨优美回忆 2024-08-13 17:21:51

假设前端和管理都位于同一文件系统上,您将需要对管理中的所有内容使用绝对路径。在管理员的配置中创建一个映射到前端的物理上传/图像文件夹的定义。例如,从前端,您可以使用相对路径 ./upload 访问 uploads 文件夹,但从 admin.example.com 站点,您将需要使用绝对路径 /user/example.com/upload。

字体的配置看起来像(www.site.com/config.php):

define("UPLOAD_FOLDER", "./uploads");
define("WEB_UPLOAD_FOLDER", "/uploads");

管理员的配置看起来像(admin.site.com/config.php):

define("UPLOAD_FOLDER", "/user/site.com/upload");
define("WEB_UPLOAD_FOLDER", "http://www.site.com/uploads");

然后前端和管理都将引用物理文件夹

$filename = UPLOAD_FOLDER . "/myupload.mp3";

:要创建上传的超链接,您可以使用以下命令:

<a href="<?php echo WEB_UPLOAD_FOLDER . "/myupload.mp3" ?>">My Upload</a>

Assuming both the frontend and the admin are on the same file system, you will need to use absolute paths for everything in the admin. In the admin's config create a define that maps to the frontend's physical upload/image folder. For example, from the fontend you can access uploads folder with the relative path ./upload but from the admin.example.com site you will be required to use the absolute path /user/example.com/upload.

The fontend's config would look like (www.site.com/config.php):

define("UPLOAD_FOLDER", "./uploads");
define("WEB_UPLOAD_FOLDER", "/uploads");

The admin's config would look like (admin.site.com/config.php):

define("UPLOAD_FOLDER", "/user/site.com/upload");
define("WEB_UPLOAD_FOLDER", "http://www.site.com/uploads");

Then both the frontend and admin would reference the physical folder with:

$filename = UPLOAD_FOLDER . "/myupload.mp3";

And to create hyperlinks to the upload you would use this:

<a href="<?php echo WEB_UPLOAD_FOLDER . "/myupload.mp3" ?>">My Upload</a>
瞄了个咪的 2024-08-13 17:21:51

另一种可能的解决方案是在 apache 中为您移动的目录定义一个目录别名。

假设您的子域根是
/subdomains/images

<VirtualHost>
...
Alias /images "/subdomains/images"
...
</VirtualHost>

www.yourDomain.com/images 和 images.yourDomain.com 都会加载相同的文件。

或者,如果您使用 Linux,符号链接也可以完成同样的事情。

Another possible solution would be to define a directory alias in apache for the directories you've moved.

Lets say your sub domain root is
/subdomains/images

<VirtualHost>
...
Alias /images "/subdomains/images"
...
</VirtualHost>

Both www.yourDomain.com/images and images.yourDomain.com would load the same files.

Or, if your using linux, a symlink could accomplish the same thing.

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