How to Handle the Warning of File_Get_Contents() Function in PHP

How can I handle the warning of file_get_contents() function in PHP?

Step 1: check the return code: if($content === FALSE) { // handle error here... }

Step 2: suppress the warning by putting an error control operator (i.e. @) in front of the call to file_get_contents():
$content = @file_get_contents($site);

How to catch the error of file_get_contents() in php

file_get_contents() returns FALSE on failure. You must check for that value with the === operator. If you wish to suppress the warning, you can use the @ operator in front of file_get_contents().

$contenido = @file_get_contents($url);
if (contenido === FALSE) {
$save=FALSE;
}

From the file_get_contents() docs:

Warning
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. Use the === operator for testing the return value of this function.

file_get_contents good way to handle errors

Try cURL with curl_error instead of file_get_contents:

<?php
// Create a curl handle to a non-existing location
$ch = curl_init('http://404.php.net/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = '';
if( ($json = curl_exec($ch) ) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}

// Close handle
curl_close($ch);
?>

Unable to catch PHP file_get_contents error using try catch block

try/catch doesn't work because a warning is not an exception.

You can try this code so you can catch warnings as well.

//set your own error handler before the call
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
}, E_WARNING);

try {
$url = 'http://wxdex.ocm/pdd.jpg';
$file_content = file_get_contents($url);
} catch (Exception $e) {
echo 'Error Caught';
}

//restore the previous error handler
restore_error_handler();

How to handle file_get_contents warning?

i suggest using is_file(file)

 <?php
if (empty($_POST["name"])){
echo '';
}else{
echo '';
}
$name = $_POST["name"];
if (! is_file("./files/" . $name)){
echo '';
}else{
echo "<div id='html1' >";
echo file_get_contents("./files/" . $name,"UTF-8");
echo "</div>";
}
?>

file_get_contents() handling error message

You need to check whether the call to file_get_contentswas successful:

$t = microtime( TRUE );
@$content = file_get_contents( "http://www.example.org" );
if($content === FALSE) {
print "Site down"; // or other problem
} else {
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
}

The @ is there to suppress the warning. Also Note the ===.

File_get_contents not evaluating to false when file does not exist

file_get_contents() generates an E_WARNING level error (failed to open stream) which is what you'll want to suppress as you're already handling it with your exception class.

You can suppress this warning by adding PHP's error control operator @ in front of file_get_contents(), example:

<?php

$path = 'test.php';
if (@file_get_contents($path) === false) {
echo 'false';
die();
}

echo 'true';

?>

The above echoes false, without the @ operator it returns both the E_WARNING and the echoed false. It may be the case that the warning error is interfering with your throw function, but without seeing the code for that it's hard to say.



Related Topics



Leave a reply



Submit