dongtang6775 2011-12-16 19:00
浏览 55
已采纳

PHP访问已被锁定以进行编辑的文件 - 权限被拒绝错误

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?

  • 写回答

2条回答 默认 最新

  • douqian1296 2011-12-16 19:10
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?