2

I'm doing some csv parsing, and if I cancel the parsing (by navigating to a different page or refreshing) before the fclose() function gets called, I get the below error when I go back and start up the program again:

Warning: unlink(spreadsheet.csv) [function.unlink]: Permission denied

When I try to open the spreadsheet.csv file in excel, I get a "File in Use" error, saying "spreadsheet.csv is locked for editing by 'another user.'" I understand that my php server is currently stuck in a state of trying to access the file. Is there a way I can use php to check if the file is locked for editing, and unlock it?

user954912
  • 243
  • 1
  • 5
  • 12

2 Answers2

0

Take a look at flock().

I use it like:

if (!$file->flock(LOCK_EX)) {
    throw new Exception('Unable to obtain lock on file');
}
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
0

You can try to unlock a file with php like this:

$rFile = fopen("path/file.txt", "r+");
flock($rFile, LOCK_UN); 
fclose($rFile);

And the best way to get read-write access to the file is this:

$iOldumask = umask(0);
chmod("path/file.txt", 0777);
umask($iOldumask);
noob
  • 8,982
  • 4
  • 37
  • 65
  • good advice, but I figured out my problem. My web page uses an ajax call to return data from a very long php script, so even if I exit the page early, that php script is still being carried out. Is there a way I could tell the server to stop trying to execute the previous ajax request, if there still is one running in the background? – user954912 Dec 16 '11 at 19:36
  • Yes, look [here](http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery) – noob Dec 16 '11 at 20:04