Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents() error

I am using file_get_contents on my PHP and it throws some errors:

My code

#try to fetch from remote
$this->remotePath = "http://some-hostname.com/blah/blah.xml
$fileIn = @file_get_contents($this->remotePath);

The errors:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /virtual/path/to/file/outputFile.php on line 127

Warning: file_get_contents(https://some-host-name/data/inputFile.xml) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /virtual/path/to/file/outputFile.php on line 127

Any idea? It worked fine in my computer but stopped working when I ported it to the web server.

like image 497
Tu Hoang Avatar asked Jul 01 '11 17:07

Tu Hoang


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What will the file_get_contents () return?

Return Values ¶ The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

What is the difference between file_get_contents () function and file () function?

The file() function reads the entire file in a array, whereas file_get_contents() function reads the entire file into a string.

Does file_get_contents cache?

file_get_contents() does not have caching mechanism. However, you can use write your own caching mechanism. UPDATE the previous if case is incorrect, now rectified by comparing to current time.


1 Answers

Your server must have the allow_url_fopen property set to true. Being on a free webhost explains it, as it's usually disabled to prevent abuse. If you paid for your hosting, get in contact with your host so they can enable it for you.

If changing that setting is not an option, then have a look at the cURL library.

like image 196
Tim Cooper Avatar answered Sep 21 '22 07:09

Tim Cooper