1

I've got several functions in my php app that relay on calls to file_get_contents(), file_put_contents, and getimagesize().

The problem is that when allow_url_fopen an allow_url_include are disabled in php.ini I'm getting errors on these critical functions.

Warning: getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration in /home/content/.../html/_sites/mysite/wp-content/themes/mytheme/functions.php on line 2534

What are the options for working around these issues?

EDIT: These are all local files on the same webserver as the calling php script. Is there a preferred method for reading/writing file contents?

RegEdit
  • 2,134
  • 10
  • 37
  • 63
  • If [`cURL`](http://php.net/manual/en/book.curl.php) is enabled on the server you could transfer it to the local file system and operate on the file locally. If it's not enabled, you're pretty stuck. – DaveRandom Apr 18 '12 at 12:12
  • EDIT: These are all local files on the same webserver as the calling php script. Is there a preferred method for reading/writing file contents? – RegEdit Apr 18 '12 at 12:18
  • Uhm, how about using their file-system local paths then instead of http: URLs? – mario Apr 18 '12 at 12:20
  • 1
    @RegEdit if they `are all local files on the same webserver`, why are you using paths that require `allow_url_fopen` to be enabled? Just use local file system paths and none of this will be an issue... – DaveRandom Apr 18 '12 at 12:21

2 Answers2

3

You can use curl to get remote pages. You can store the results of the curl request to a variable and echo() in place of using the url wrapper to fetch content.

In theory, you could also eval() the returned data. But running remotely fetched PHP code is a ENORMOUS security risk, any PHP code included in this way can literally do anything you can. DON'T DO IT! The same goes for allow_url_include too

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Thanks Gordon. These are all local files on the same webserver as the calling php script. Is there a preferred method for reading/writing file contents? – RegEdit Apr 18 '12 at 12:18
  • 1
    If the files you want to include are on the same server, then just include() or fopen() them directly. No need for going through a URL and adding the overhead that doing it that way would involve. – GordonM Apr 18 '12 at 12:21
  • Thanks for clearing that up Gordon, I was mistakenly calling the file as http instead of ABSPATH. – RegEdit Apr 18 '12 at 12:52
1

If you have access to your webserver, you may have to find your php.ini file, for example:

/etc/php5/apache/php.ini

And use these options:

; http://php.net/allow-url-fopen
allow_url_fopen = On

; http://php.net/allow-url-include
allow_url_include = Off

If your using someking of hosting account, these options may be interactively available at a control panel.

The second solution might be to use cURL. I suppose you were trying to call getimagesize with an URL. Documentation here: http://php.net/manual/en/book.curl.php

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
ther
  • 848
  • 8
  • 16