Support - Knowledge base

Giving you the answers you need

Why not try our knowledge base for instant help and advice to enhance your online experience with Register365? From within the knowledge base you can browse our frequently asked questions and find the right answers to help solve your queries.

Home  >  Scripting  >  PHP (Hypertext Preprocessor)  >  View Article

Fopen (PHP) on Windows

3*3*3*3*3*

Article

In a measure to improve security, Hosting365 has disabled the PHP option 'allow_url_fopen'. This would normally allow a programmer to open, include or otherwise use a remote file using a URL, rather than a local file path.

The "cURL" library provides a feature rich alternative.

We have provided some examples in this document.

Fetching a web page:

<?php
$ch 
curl_init();
curl_setopt($chCURLOPT_URL"http://example.com/");
curl_setopt($chCURLOPT_HEADER0);
curl_exec($ch);
curl_close($ch);
?>

Alternative for file_get_contents()

Instead of:

<?php
$file_contents 
file_get_contents('http://example.com/');
// display file
echo $file_contents;
?>

Use this:

<?php
$ch 
curl_init();
$timeout 5;
// set to zero for no timeout
curl_setopt ($chCURLOPT_URL'http://example.com');
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
$file_contents curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>

However, if you receive errors you can use this:

<?php
$site_url 
'http://example.com';
$ch curl_init();
$timeout 5;
// set to zero for no timeout
curl_setopt ($chCURLOPT_URL$site_url);
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
ob_start();
curl_exec($ch);
curl_close($ch);
$file_contents ob_get_contents();o
b_end_clean
();
echo 
$file_contents;
?>

Alternative for file()

Instead of:

<?php
$lines 
file('http://example.com/');
// display file line by line
foreach($lines as $line_num => $line) {
    echo 
"Line # {$line_num} : ".htmlspecialchars($line)."\n";
}
?>

Use this:

<?php
$ch 
curl_init();
$timeout 5;
// set to zero for no timeout
curl_setopt ($chCURLOPT_URL'http://example.com');
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
$file_contents curl_exec($ch);
curl_close($ch); $lines = array();
$lines explode("\n"$file_contents);
// display file line by line
foreach($lines as $line_num => $line) {
    echo 
"Line # {$line_num} : ".htmlspecialchars($line)."\n";
}
?>

Rate This Article

How useful was this article?

Not useful A little useful Useful Very useful Everything I needed