My problem is related to the php.ini
file I think, but it might be something else.
Let's say we have a site architecture where inside the root directory there is another directory named img
and a file named index.php
. Inside the directory img
there is a file named image.jpg
.
So in index.php
, to refer to an image, I would use /img/image.jpg
.
My question is, what should I change in php.ini
to be able to write img/image.jpg
instead of /开发者_如何转开发img/image.jpg
.
Thanks
Relative paths get resolved by the web browser, not by PHP. There is no way to override this behavior on the web server.
Nothing PHP can do. You could essentially do that with Apache rewrites but it makes much more sense to just use absolute paths. You should not try to mix absolute and relative paths because it will only cause problems for you in the future. What's wrong with adding the '/' to the front of the path?
Not only that, but it will confuse the hell out of people trying to debug your code that all of your relative paths are becoming absolute paths in Apache.
My question is, what should I change in php.ini to be able to write
img/image.jpg
instead of/img/image.jpg
.
This is answering your question but not the answer you would like to hear. It quite simple, let's first list the ini settings:
auto_prepend_file
(probably in conjunction withinclude_path
)
And that's all. How it works:
Prepend a php script with the ini directive. inside that file, you start output buffering with your own callback function:
function callback($buffer)
{
// slash that image that you want to write without slash
return (str_replace("img/image.jpg", "/img/image.jpg", $buffer));
}
ob_start("callback");
You are now able to write img/image.jpg
instead of /img/image.jpg
because your output filter function does re-write it for you.
But take care that you're not trying to fix the wrong end with this. General rule of hypertext references apply, see Uniform Resource Identifier (URI): Generic Syntax , Hypertext Transfer Protocol -- HTTP/1.1 and Hypertext Markup Language - 2.0 .
精彩评论