I open a Microsoft Access file normally and I can see its locked by Micorsoft Office. I then attempt to check if its writeable with PHP:
echo '|--> '.is_writeable('C:\wamp\www\Database1.accdb');
But it returns a 1. Surley, it should return a 0 when open?
Just to test, I then attempt to write to it:
$fh = fopen('C:\开发者_JS百科wamp\www\Database1.accdb', 'w+');
fwrite($fh, 'hello');
It lets me do so! Is there anyway I can make sure if a file is not open by another program?
When you open a Microsoft document regardless weather its a word, power-point or access file the application will create a file within the same directory that is marked as hidden, this file is what tell's the office application that the file is being used.
the file in question is NOT Locked by the operating system therefore the OS Cannot determine weather the file is opened by an MS Application.
What you have to do is check to see if the lock file exists, this then will allow you to determine weather the document is currently open by the associated application.
Something like:
if(is_writeable("file.accdb") && !file_exists("file.lccdb"))
{
touch("file.lccdb"); //mimic the file is being used
/*
* do Work
*/
unlink("file.lccdb"); //Remove it
}
You can check whether it's open with Access, as (for 2007 databases at least) Access puts a file with a "laccdb
" extension in the same folder with the same name as the database. Obviously this may not be the case with all programs.
If the only programs that are likely to open it are Access and ones you've made, you could ensure that every time you open it, you also create a "database.laccdb
" file in the same directory at the same time, and then every time you open it check to make sure that file doesn't exist. I think the best way is to simply check if the lock file is there, as if it is, then it is probably open (unless the program that last used it forgot to delete it, for example, unexpected shutdown,) and if it isn't there, then there shouldn't be any programs reading from it.
The non-MSAccess-specific answer is:
Programs are supposed to lock files while they are opened so other programs cannot write to them if they don't want such behaviour. If the program that opens the file fails to do so, you cannot detect if it opened the file without writing C/C++ code that uses WinAPI functions to detect which programs have active file handles for your file.
精彩评论