Is there an alternative to the file_get_contents () function?

On my web hosting server the feature is file_get_contents()disabled. I am looking for an alternative. please, help

+3
source share
9 answers

file_get_contents () pretty much does the following:

$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

Since file_get_contents () is disabled, I'm sure the above will not work.

Depending on what you are trying to read, and in my experience hosts usually disable reading of deleted files, you may have other options. If you are trying to read deleted files (over the network, i.e. http, etc.), you can look in the library functions in cURL

+4
source
+3

.

function ff_get($f) {
        if (!file_exists($f)) { return false; }
        $result = @file_get_contents($f);
        if ($result) { return $result; }
        else {
            $handle = @fopen($f, "r");
            $contents = @fread($handle, @filesize($f));
            @fclose($handle);
            if ($contents) { return $contents; }
            else if (!function_exists('curl_init')) { return false; }
            else {
                $ch = @curl_init();
                @curl_setopt($ch, CURLOPT_URL, $f);
                @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $output = @curl_exec($ch);
                @curl_close($ch);
                if ($output) { return $output; }
                else { return false; }}}}
+3

, file_get_contents() , , . code_burgar , .
, file_get_contents() ( -replacement) , , . SplFileObject . , .

+2

:

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
+2

, http:// ftp://.

, fread() , , fsockopen(). , , .

+1

PEAR. PHP- PHP- .

require_once 'PHP/Compat.php';
PHP_Compat::loadFunction('file_get_contents');

, , .

require_once 'PHP/Compat/Function/file_put_contents.php';

  • if(!function_exists()), , webhoster .
  • , PHP, !
0
source

If all you are trying to do is initiate a hit on the given URL and you don’t need to read the output you can use curl (), provided that your website is turned on on your server.

The documentation here gives an example of calling url using curl.

0
source

If all else fails, there is always cURL. There was a good opportunity to install.

0
source

Source: https://habr.com/ru/post/1724016/


All Articles