dounao5856 2010-04-27 06:51
浏览 108

无法打开文件进行写入

I am trying to write to a file. I do a file_exists check on it before I do fopen and it returns true (the file does exist).

However, the file fails this code and gives me the error every time:

$handle = fopen($filename, 'w');
if($handle)
{
    flock($handle, LOCK_EX);
    fwrite($handle, $contents);
}
else
{
    echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
    exit();
}
flock($handle, LOCK_UN);
fclose($handle);

Is there a way I can get more specific error details as to why this file does not let me open it for writing? I know that the filename is legit, but for some reason it just wont let me write to it. I do have write permissions, I was able to write and write over another file.

  • 写回答

4条回答 默认 最新

  • douliexing2195 2010-04-27 06:54
    关注

    If you're using php 5.2+ you might be interested in error_get_last().
    On your development system you can also increase the error reporting level, either within the script via error_reporting() or (preferably) in your php.ini.

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $handle = fopen($filename, 'w');
    if(!$handle) {
      echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
      var_dump(error_get_last());
      exit();
    }
    
    flock($handle, LOCK_EX);
    fwrite($handle, $contents);
    flock($handle, LOCK_UN);
    fclose($handle);
    
    评论

报告相同问题?