Page 1 of 1

Having trouble with fopen() inside of ExpressionEngine

Posted: Sun Jan 11, 2009 11:25 pm
by vossavant
I've done a lot of homework on this - asked at the ExpressionEngine (EE) forums and more - and I'm hoping someone here has the brains to solve this.

I have an IP location script that works fine when called outside of EE (output viewable here: http://www.psaudio.com/IPTEST/sample-IP2Location.php). It returns the IP address, the country code, and the country long name just fine. The IP is grabbed using the standard getenv() function, while the country code and country long name are taken from a BIN file. The BIN file is in the same directory as the script.

When I call the script within EE, the IP is detected just fine, but the BIN file fails to load (output viewable here: http://www.psaudio.com/ps/home/zz_test_ip/).

Here is how I'm calling the script from within EE:

Code: Select all

include("/home/psaudio/public_html/IPTEST/sample-IP2Location.php");
Note that I'm calling the exact same script that works perfectly well outside of EE. I've determined that no proprietary EE tags are getting in the way, so it is probably a path issue.

Here is the function call (within the script I'm calling) that loads the BIN file:

Code: Select all

$ip = IP2Location_open("IP-COUNTRY.BIN", IP2LOCATION_STANDARD);
And here is the function:

Code: Select all

function IP2Location_open ($filename, $flags) {
    $ip = new IP2Location;
    $ip->flags = $flags;
    $ip->filehandle = fopen($filename,"rb");
    if ($ip->filehandle === false) {
        echo FILEHANDLE_NULL . $filename . ".\n";;
        return 0;
    }
    $ip = IP2Location_initialize($ip);
    return $ip;
}
The error indicates that the file is failing to load, but why? (you can see the error by visiting the links above) I've tried adjusting the paths in the function call and within the function, but have had no success. I've also checked file permissions - they are set to 644. I tried 777 just to check, but that made no difference.

Can anyone help?

Re: Having trouble with fopen() inside of ExpressionEngine

Posted: Sun Jan 11, 2009 11:30 pm
by requinix
When you say "IP-COUNTRY.BIN" that's relative to the current working directory, not the directory of the file currently executing.

In other words you have to specify the path to that file in absolute terms:

Code: Select all

$ip = IP2Location_open("/home/psaudio/public_html/IPTEST/IP-COUNTRY.BIN", IP2LOCATION_STANDARD);

Re: Having trouble with fopen() inside of ExpressionEngine

Posted: Mon Jan 12, 2009 11:08 am
by vossavant
Hey tasairis,

Thanks for the response. I tried that, but still receive the same error. Here's what I have inside the script:

Code: Select all

$ip = IP2Location_open("/home/psaudio/public_html/IPTEST/IP-COUNTRY.BIN", IP2LOCATION_STANDARD);
Do you have any other ideas?