如果在不使用try / catch的情况下在PHP中使用fopen,如何捕获“权
发布时间:2020-12-13 21:48:18 所属栏目:PHP教程 来源:网络整理
导读:当脚本尝试使用“w”(写入)模式打开新文件时,我刚收到有关权限被拒绝错误的 one of my scripts错误报告.这是相关的功能: function writePage($filename,$contents) { $tempfile = tempnam('res/',TINYIB_BOARD . 'tmp'); /* Create the temporary file */ $
当脚本尝试使用“w”(写入)模式打开新文件时,我刚收到有关权限被拒绝错误的
one of my scripts错误报告.这是相关的功能:
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 */ } 你可以看到第三行是我使用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'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |