Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

--TEST-- Test fscanf() function: error conditions --FILE-- <?php echo "*** Testing fscanf(..

Decoded Output download

*** Testing fscanf() for error conditions ***

Did this file decode correctly?

Original Code

--TEST--
Test fscanf() function: error conditions
--FILE--
<?php
echo "*** Testing fscanf() for error conditions ***\n";
$file_path = __DIR__;

$filename = "$file_path/fscanf_error.tmp";
$file_handle = fopen($filename, 'w');
if ($file_handle == false)
  exit("Error:failed to open file $filename");
fwrite($file_handle, "hello world");
fclose($file_handle);

// invalid file handle
try {
    fscanf($file_handle, "%s");
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}

// number of formats in format strings not matching the no of variables
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
  exit("Error:failed to open file $filename");
try {
    fscanf($file_handle, "%d%s%f", $int_var, $string_var);
} catch (ValueError $exception) {
    echo $exception->getMessage() . "\n";
}
fclose($file_handle);

// different invalid format strings
$invalid_formats = array("", "%", "%h", "%.", "%d%m");


// looping to use various invalid formats with fscanf()
foreach($invalid_formats as $format)  {
  $file_handle = fopen($filename, 'r');
  if ($file_handle == false)
    exit("Error:failed to open file $filename");
  try {
    var_dump(fscanf($file_handle, $format));
  } catch (ValueError $exception) {
    echo $exception->getMessage() . "\n";
  }
  fclose($file_handle);
}

echo "\n*** Done ***";
?>
--CLEAN--
<?php
$file_path = __DIR__;
$filename = "$file_path/fscanf_error.tmp";
unlink($filename);
?>
--EXPECT--
*** Testing fscanf() for error conditions ***
fscanf(): supplied resource is not a valid File-Handle resource
Different numbers of variable names and field specifiers
array(0) {
}
Bad scan conversion character "
Bad scan conversion character "
Bad scan conversion character "."
Bad scan conversion character "m"

*** Done ***

Function Calls

None

Variables

None

Stats

MD5 552b29b216180f9f8cc2a9be3ddb40ef
Eval Count 0
Decode Time 83 ms