Skip to content
PHP Logo

What alternative methods can be used to fetch a file from a server without allow_url_fopen?

When the PHP directive `allow_url_fopen` is disabled, it prevents the use of functions like `file_get_contents()` to fetch files from remote servers. To work around this limitation, you can use cURL or streams to retrieve the file contents.

php
// Using cURL to fetch a file from a server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/file.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$file_contents = curl_exec($ch);
curl_close($ch);

// Using streams to fetch a file from a server
$context = stream_context_create(array(
    'http' => array(
        'method' => 'GET'
    )
));
$file_contents = file_get_contents('http://example.com/file.txt', false, $context);