开发者

Translating server file paths to URLs in Codeigniter

I'm developing my first application in Codeigniter 2.0.2, and I've got a minor issue I can't seem to find any info about on the web.

The application revolves around resources stored locally on the server (namely images and audio files) which need to be exposed to the user. The locations of these resources are stored in a DB as absolute paths. As a result, I find myself needing to translate server paths to base_url-based URLs fairly often.

I already wrote a simple and robust-en开发者_高级运维ough function to handle it for me, but with all the seemingly-relevant helpers in CI (url, path), I can't shake the feeling that I just reinvented the wheel.

At the least, I would think there would be some kind of CI function (say, "basify") that will translate any server path to a base_url-relative path like so:

$server_path = '/server/path/to/app/resources/image.jpg';
basify( $server_path );  // returns 'resources/image.jpg' or './resources/image.jpg'

where the CI app lives in /server/path/to/app. Then it's a simple call to base_url() to make the URL.

Does anything like that exist?

Edit: And yes, I know that a simple preg_replace will handle most cases for me (at least those where the resource is within the base path), but I feel like this should be CI's job, not mine! Half the reason I'm using it is because I don't want to think about path management.


Maybe you can use php function str_replace together with codeigniter FCPATH constant.
Your code will be:

function basify($image_path) {
    return str_replace(FCPATH, '', $image_path);
}

The FCPATH constant is new to me, but I have done some tests and it works fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜