Sometimes direct urls don't work while partials do
Moderator: General Moderators
Sometimes direct urls don't work while partials do
Recently I ran into a problem where a code that creates png images stopped working. After a long while of testing, it turned out that if I take the domain name out of the url and just write the path as graphics/filename.png, then it works. How is it possible that it was working before with the full url and then it stopped working? Overall it's a total hassle when to use relative paths and when the opposite.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Sometimes direct urls don't work while partials do
Are you rewriting URLs? That may cause the change. Have you checked the logs to see what the actual URLs are? Perhaps your full URLs are not actually correct.
(#10850)
Re: Sometimes direct urls don't work while partials do
At one point I had this thing that it generates the file path by combining the domain url (which is in a class variable called $rootUrl) and the folder/subfolder/filename.png, then does $filecheck = @getimagesize($filepath); to determine if the file exists. If $filecheck is false then it returns an error code and the script is aborted. However, later on when I use the same path ($filepath) to access the image, it says the image doesn't exist, even though it does and I can access it by copypasting the url to the address line. Once I took the domain name out of the equation, it works just fine. On my own computer, I was originally using file_exists() but I learned that's not supposed to be used with urls, only files on your hard drive, so I learned of the @getimagesize() thing. Or does @getimagesize() actually return false when the file is inaccessible? Because if it doesn't then it obviously wouldn't work, and that would explain everything.
Re: Sometimes direct urls don't work while partials do
There's two reasons not to do that:Seeeks wrote:I was originally using file_exists() but I learned that's not supposed to be used with urls
1. file_exists, and other similar functions, are supposed to work with actual file paths. There is some support for checking if a URL "exists", but if you need to do that then there are better and more reliable methods.
2. If the URL is one to your own server then you should always use the actual file path on your server. Not the URL. Besides the reason above, it's terribly inefficient: PHP has to do a request to your server, the server has to figure out what the URL means, look up the corresponding file, return the file as its response, then PHP does some logic on that response to decide what it should return from the function call. When you use an actual file path you bypass most of that process and will get the result much faster.
Instead of prepending the domain name, use the DOCUMENT_ROOT: it's a file path that corresponds to the "root" of your website, just like how your $rootUrl is a (partial) URL that corresponds to the "root" of your website.
Code: Select all
// bad: does a web request to your own server and is very slow
// $filepath = $rootUrl . "/folder/subfolder/filename.jpg";
// good: look at the file directly as it's stored on the server
$filepath = $_SERVER["DOCUMENT_ROOT"] . "/folder/subfolder/filename.jpg";Re: Sometimes direct urls don't work while partials do
I need to check if certain image files exist, because they are created when a character spends action points to unlock them, and after this the data contained them is unlocked for all the users. The files are never shown directly to anybody, but the color of the pixels in them symbolizes different things and they are expressed to the users in another forms. If there is no file where the filepath points, the code will generate a new file in the location specified by the filepath, following certain rules while being otherwise random. I think the reason I was confused with $_SERVER["DOCUMENT_ROOT"] earlier was that I didn't realize that the filepath is different from an url, so you can't use $_SERVER["DOCUMENT_ROOT"] in links (I assume?) But when dealing with files in the background, sounds like it's what's supposed to be used.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Sometimes direct urls don't work while partials do
Yes, use $_SERVER["DOCUMENT_ROOT"] for the file path to the public directory. For URLs, I would recommend defining your own configuration variable. You can use $_SERVER["SERVER_NAME"] and $_SERVER["SCRIPT_NAME"], but there are conditions where they are unsafe and get their information from the request header.Seeeks wrote: so you can't use $_SERVER["DOCUMENT_ROOT"] in links (I assume?) But when dealing with files in the background, sounds like it's what's supposed to be used.
(#10850)