File_exists or getimagesize only work with local, absolute file paths, but not with URLs in PHP

I recently upgraded one of my servers, and since then I have had a problem with some specific PHP commands (see below). I believe this is a configuration problem, but I have already covered a few things and donโ€™t know anymore. So maybe you have a good idea:

I use the following code to display a standard logo on an intranet site or a user-defined logo:

if(L_HEADER) { $logo = L_HEADER; } else { $logo = 'logo.png'; } $properties = getimagesize(CONFIG_URL . 'images/' . $logo) 

L_HEADER and CONFIG_URL are constants with predefined values โ€‹โ€‹(another file):

Concatenation works correctly, which is also confirmed by the Apache log file error message:

PHP Warning: getimagesize ( http: //billing.intranet.opb/images/opb_beta.png ) : Could not open stream: HTTP request failed! HTTP / 1.1 404 NOT FOUND in /var/www/billing/templates/header.inc.php on line 42

So, the first obvious conclusion: the path is wrong. But this is not so, believe me. I tested it as 1,000 times. In fact, the first curiosity is that the image is displayed and correctly refers to a couple of lines further in the code of the same file:

 echo '<img src="' . CONFIG_URL . 'images/' . $logo . '" width="' . $properties[0] . '" height = "' . $properties[1] . '" />"; 

As I get the error mentioned above, the height and width are โ€œ0โ€, but looking at the source code, the URL is fine, accessing it manually opens the image, and when replacing the width and height with manual values, the image is simply displayed excellent.

More curious though (and also my current finx), when changing getimagesize to the next, it works just fine:

 $properties = getimagesize($_SERVER['DOCUMENT_ROOT'] . /public_html/images/' . $logo); 

I mentioned that I am using Apache redirection; therefore, in the URL you do not see "public_html", but in the absolute path of the second example you see it.

The same thing happens with "file_exists". URL does not work, absolute local path for the same file.

Another curiosity: in another piece of code, I check online for updates. There I use a "real", public URL with file files and fopen. I look and it works great:

 if(file_exists('http://desk.co.cr/df_stable.txt') { if(($handle = fopen('http://desk.co.cr/df_stable.txt', 'r')) !== FALSE) { // some other code } } 

Now those things that I already checked:

  • File permissions are set correctly for the entire path, with www data being the group and owner of all files, and read and write access for the image file
  • allow_url_fopen in the "On" settings.
  • open_basedir is set to "no value" and there is no override in the definitions of Apache virtual hosts.
  • the file finally exists, and the syntax + path is correct.

Some background information:

  • Server runs on Ubuntu 14.04 LTS
  • Apache 2.4.7
  • PHP 5.5.9

I currently have no ideas.

+5
source share
1 answer

Buried in your question is a vital key that you have already found:

Another curiosity: in another piece of code, I check online for updates. There I use a "real", public URL

Thus, the URL that fails belongs to the non-public domain that your computer is configured to look for the correct IP address. Most likely, this was done either when starting the local DNS server, or when setting up the hosts PC file to hard-code the address for this domain.

If you request a URL from the server itself, however, the game has a completely different DNS configuration, so probably he just does not know where this server is located, even if he is! You need to configure the DNS server or /etc/hosts settings to match what is on your PC.

One related possibility is that the server is configured with the same search address, but the router does not allow it to connect to itself in this way. One way is to specify the hosts file entry in 127.0.0.1 and configure Apache to match.

If you can get the command line on the server, you can try:

 nslookup billing.intranet.opb # if that returns the right IP address, see if it reachable: ping billing.intranet.opb # and if it the right server, it will be listening on port 80: telnet billing.intranet.opb 80 # telnet will either time out, or connect and give you a prompt 

If telnet connects, you can even write an HTTP request manually (an empty line ends the request, does not require too long input, or the server crashes), for example:

 HTTP/1.1 HEAD / Host: billing.intranet.opb 
+2
source

All Articles