tags:

views:

34

answers:

2

In one's server, file_get_contents is disabled for security reasons. I need to retrieve xml data. So, what is the best thing to do to :

  1. Verify that file_get_contents is supported by a server ?
  2. Is there any subtitute methods of file_get_contents ?
+4  A: 

You can check whether or not you can use url's in file_get_contents() (and the fopen() family of functions) by checking the ini directive allow_url_fopen:

ini_get('allow_url_fopen'); 

You can get around these restrictions by using:

  1. cURL
  2. fsockopen()

I strongly recommend cURL. fsockopen() is a lot dirtier.

NullUserException
+1 beat me to it ;)
Daniel Vandersluis
+2  A: 

http://www.phpbuilder.com/board/showthread.php?t=10349521 - this seems to be a releavant topic

I would try to use simply fopen and fread functions.

First approach:

$bufferSize = 1024;
$file = fopen($file,'r');
while($cont = fread($file, $bufferSize)){
 $file_content .= $cont;
}
fclose($file);

var_dump($file_content);

Second approach:

$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
MartyIX
But the OP said the function was disabled, so if `file_get_contents` is disabled, `fopen` may not be... And the question didn't say anything about URLs at all actually (It's just assumed based on "retrieve xml data"). If `allow_url_fopen` is disabled, then yes @NullUserException, `fopen` won't work either... But it might still work given the details in the question...
ircmaxell
@NullUserException: I gathered from the question that file_get_contents may be disabled via directive `disable_functions`. You are talking in your answer about URLs but OP didn't say a word about URLs.
MartyIX
@irc, Marty I guess I misunderstood/misinterpreted the question. I removed the downvote.
NullUserException
@NullUserException, I don't think any of us understand the question. It's not really clearly worded, so either (or both) answer could be correct (or both could be wrong). We're just making assumptions based upon our interpretation of the question...
ircmaxell
@NullUserException: It is clear now. You were right ;-))
MartyIX