Jump to content

check to make sure writing to file was successful.


denoteone

Recommended Posts

I have a function to writes data I want to make sure it was successful.

 

my function:

function save_ini_file($filename,$content)
{
$File = "ini_files/". $filename . ".ini"; 

echo $File;
$Handle = fopen($File, 'w'); 
$info = $content; 
fwrite($Handle, $info); 
fclose($Handle);

  return true;
}

 

the if statement that calls the function.

if(!save_ini_file($serial,$data))
{
echo "did not write";
}else{
echo "did write";
}

 

the file is being created but I am getting the "did not write" echoed

If you want to make sure your function executes successfully you need to at least check what it is doing.

 

function save_ini_file($filename,$content)
{
  $File = "ini_files/". $filename . ".ini";
  if ($Handle = fopen($File, 'w')) {
    $info = $content;
    if (fwrite($Handle, $info)) {
      return true;
    }
    fclose($Handle);
  }
  return false;
}

 

Because there are several things that may go wrong within this function, you might also want to take look at having it throw exceptions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.