开发者

Absolute Relative path issues in referencing web assets - need help - php related

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.


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__) . '/..'));


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜