tsl*_*cum 3 php error-handling fopen permission-denied
我刚刚收到一个错误报告我的一个脚本,当脚本试图用"W"(写)模式打开一个新的文件,关于许可被拒绝的错误.这是相关的功能:
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 */
}
Run Code Online (Sandbox Code Playgroud)
你可以看到第三行是我使用fopen的地方.我想捕获权限被拒绝错误并自己处理它们而不是打印错误消息.我意识到使用try/catch块非常容易,但可移植性是我脚本的一大卖点.我不能牺牲与PHP 4的兼容性来处理错误.请帮助我捕获权限错误,而不打印任何错误/警告.
我认为您可以通过使用此解决方案来防止错误.只需添加一个额外的检查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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3268 次 |
| 最近记录: |