N
N
Nikolai Vasilchuk2011-12-22 07:39:00
PHP
Nikolai Vasilchuk, 2011-12-22 07:39:00

PHP: Check if file is writable

There is a php script that reads the contents of a directory and processes the files.
How can I find out if a file is fully uploaded to a directory or is being uploaded?

Example: I copy a movie to a directory (it takes a long time to copy), the php script gets the contents of the directory, sees a new file and tries to do something with it, but the file itself is still being copied.

fopen perfectly opens such files even for writing, is_writable, is_readable and the like, they also say that everything is fine with the file, rename moves such a file perfectly (it turns out to be broken).

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
shsweb, 2011-12-22
@shsweb

You can check file sizes. If the file size has not changed in a minute (several minutes, an hour, etc.), assume that it is completely filled.

O
Oleg Matrozov, 2011-12-22
@Mear

Hmm... and try to open such a file with an exclusive lock? Or... perhaps FTP has some setting so that it would lock such a file for the duration of the recording.

L
lacki, 2011-12-22
@lacki

I don't see any options other than how to lsof directories as root. running only lsof as root without entering a password can be implemented via sudoers.

M
Max, 2011-12-22
@AloneCoder

Use checksums or copy with temporary extension

E
ertaquo, 2011-12-22
@ertaquo

Copy under one name (for example, by adding a dot or underscore to the beginning of the file name), write in the script that such files should be skipped. Rename the file after copying is complete.

@
@resurection, 2011-12-22
_

I suspect that the FTP server writes to a local file in batches, buffering the incoming data and periodically dumping it to disk. If my assumption is correct, then between flushes, the file is not locked. If so, then, in the FTP server settings, there may be a corresponding checkbox to block the file throughout the entire upload process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question