4

スクリプトが 'w' (書き込み) モードを使用して新しいファイルを開こうとすると、アクセス許可が拒否されたというエラーに関するスクリプトの 1 つに関するエラー レポートを受け取りました。関連する関数は次のとおりです。

function writePage($filename, $contents) {
    $tempfile = tempnam('res/', TINYIB_BOARD . 'tmp'); /* Create the temporary file */
    $fp = fopen($tempfile, 'w');
    fwrite($fp, $contents);
    fclose($fp);
    /* If we aren't able to use the rename function, try the alternate method */
    if (!@rename($tempfile, $filename)) {
        copy($tempfile, $filename);
        unlink($tempfile);
    }

    chmod($filename, 0664); /* it was created 0600 */
}

3 行目で fopen を使用していることがわかります。エラーメッセージを出力するのではなく、許可が拒否されたエラーをキャッチして自分で処理したいと思います。これは、try/catch ブロックを使用すると非常に簡単ですが、移植性は私のスクリプトの大きなセールス ポイントです。エラーを処理するために PHP 4 との互換性を犠牲にすることはできません。エラー/警告を出力せずに許可エラーをキャッチするのを手伝ってください。

4

1 に答える 1

9

この解決策を使用することで、エラーを防ぐことができると思います。tempnam行の後に追加のチェックを追加するだけです

$tempfile = tempnam('res/', TINYIB_BOARD . 'tmp'); 

# Since we get the actual file name we can check to see if it is writable or not
if (!is_writable($tempfile)) {
    # your logic to log the errors

    return;
}

/* Create the temporary file */
$fp = fopen($tempfile, 'w');
于 2013-06-07T04:13:46.280 に答える