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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.