Search code examples
phpfilefopenexitfwrite

files created by PHP deleted on exit


I am writing some files in the filesystem, but unfortunately these files are deleted at the exit of the PHP script.

I have tried to create the files both with fopen (w+) and file_put_contents. Files are regularly deleted.

Any ideas?

EDIT: Files are written to /home/ontw/webshop/definitions1/, which is not a temporary directory.

some code:

$fp = fopen($filename, "w+");

if ($fp == false) {
  throw new RuntimeException("Could not create $filename (fopen error)!");
}

if (flock($fp, LOCK_EX)) {
  if (false === fwrite($fp, $contents)) {
    throw new RuntimeException("Could not create $filename (fwrite error)!");
  }

  fflush($fp);
  flock($fp, LOCK_UN);
} else {
  throw new RuntimeException("Could not create $filename (unable to get file lock)!");
}
fclose($fp);

Solution

  • I found the bug. The deletion was performed inside a __destruct statement. It seems that all the destructors are called on exit()