Student
Student

Reputation: 31

Is it safe to clear a file only with: fopen() and fclose()?

I found this simple function that simply clears the contents of a file:

function createEmptyFile($filename)
{
    //
    $fp = fopen($filename, 'w');
    //
    fclose($fp);
}

For example, resets the log, or something else.

But I have a question whether this function is really 100% safe, that is, its behavior is uniquely determined, and the result will be as expected.

I have a strange feeling that there is a possibility that the file: may not be cleaned, or may be damaged, or filled with random data in some case.

I understand that perhaps this is all prejudice, but I would like to know exactly how right it is. In particular, I am confused by the absence of any command between opening and closing the handle.

For example, I assume that if the PHP interpreter will perform optimization, then due to the lack of a command, between opening and closing the handle, it may skip this block, or something like that.

Sorry if I create a panic in vain.

Upvotes: 2

Views: 591

Answers (1)

Melvin
Melvin

Reputation: 1041

I am confused by the absence of any command between opening and closing the handle.

Let me put something to the front: The left '{' and the right '}' bracket are flow-controls and need commands in between. fopen() and fclose() are not. Though these are only 2 commands they inherit many more.

That said lets look into the tasks of them: fopen() does ...

  • (a) look for the specific file,
  • (b) if the file is not existing it creates the file
  • (c) opens the file for writing only,
  • (d) redirects the file-pointer to the beginning of the file,
  • (e) if the file is longer then zero it will truncate it to zero length.

In particular, I am confused by the absence of any command between opening and closing the handle.

You see there are many "commands" in between. So don't worry.

I have a strange feeling that there is a possibility that the file: may not be cleaned

To be exact, the file is not cleared, it is truncated to zero length. The earlier data of this file is still in the memory of your data-storage. But this is subject to your operation-system. There exist programs to delete these data-blocks entirely.

[...], or may be damaged

I don't understand the question. You are going to delete a file - what more damage do you expect?

[...], or filled with random data in some case.

That is C-style for creating variables by setting up a pointer to free memory but not clearing the data earlier was in it and giving this duty to you. But here it is just a truncate and not a redirecting of the file pointer. This fear could be ignored.

But I have a question whether this function is really 100% safe, that is, its behavior is uniquely determined, and the result will be as expected.

Yes, normally the behavior is uniquely determined. But you have to expect some side-effects:

  • if the file does not exist it will be created. You need to have write-access to the directory. (else it will come back with false)
  • if you have no write-access to the file it will come back with false.
  • if your php-environment uses "safe mode" the possible difference of owner and user leads to a fail of fopen(). You need to be sure that the file is worked only by you.

It can happen that you just write fopen() and don't check the return-parameter. That may cause a problem and lead to: not 100% safe if you don't react correctly.

So - yes fopen() and fclose() are sufficient, correct and inform you if the work is not done properly.

Upvotes: 3

Related Questions