Frage

I am trying to overwrite a file on a windows server. The file contents does get over written, but the file date remains unchanged.

I have tried the following;

unlink(DUMP_FILENAME); 
file_exists(DUMP_FILENAME);

This returns false, so file should be gone, correct?

file_put_contents(DUMP_FILENAME,$data,0); 

File gets new data, but date is the same as prior to the unlnk() Why would that be?

Edit: Tried deleting the file from windows, the date on the file after the PHP script is run remains as prior to the windows delete!

The reason I was trying to delete first, rather than just overwriting, was I wanted the date of the file to change.

War es hilfreich?

Lösung 2

Assuming you mean the file's creation date, this is a known behaviour of the Windows file system. If you delete, move, or rename a file and then recreate it (within some small period of time) it will magically inherit the previous creation timestamp.

While this is surprising, it is usually the sensible thing to do, e.g., this sequence of events is typical for a text editor when you save your changes, and clearly the creation date of a file shouldn't change just because you've modified it.

Possible workarounds;

  • Create the new file with a unique name, then move it over top of the original file. It must be a genuine atomic move (i.e., a call to MoveFileEx with MOVEFILE_REPLACE_EXISTING, or equivalent) rather than deleting the old file and renaming the new one. I don't know whether or not you can do this with PHP.

  • Explicitly set the creation date yourself (SetFileInformationByHandle). Again, no idea whether PHP allows you to do this.

  • Wait long enough between deleting the file and recreating it. I don't know how long "long enough" is and it might vary between different Windows versions or even within a Windows version for unpredictable reasons, so unless you can find some detailed documentation doing it this way probably isn't reliable.

  • Don't bother; make sure you use the modification date rather than the creation date when applying whatever logic it is that cares about the timestamps.

Andere Tipps

From the manual (First comment), I’ve noticed this too in the past.

The unlink() is not about removing file, it’s about removing a file name to the body of data. The manpage should say: unlink - delete a name and possibly the file it refers to.

Most of the time a file has just one name — removing it will also remove (free, deallocate) the `body' of file. That's the simple, usual case.

But a file’s body may also be kept alive' (still using diskspace) by a process holding the file open.

So basically, the body (contents) of the file will not be deallocated (will not free disk space) as long as the process holds it open. (The current thread that wrote to it)

So instead of using filectime() to check for changes and creation, use filemtime().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top