简体   繁体   English

PHP检查取消链接是否返回false或true

[英]Php check if unlink returns false or true

I want to check wether unlink returns true or false. 我想检查是否取消链接返回true或false。

Currently I tried this: 目前,我尝试了以下方法:

if(!unlink("$directory/$file"))
{
     echo "works not";
}
else
{
     echo "works";
}

I get not output, I just get this message: 我没有得到输出,我只得到以下消息:

unlink("path"...VTS_01_1.VOB): Permission denied

But it does not work, any ideas? 但这行不通,有什么想法吗?

You may use is_writable function before trying to unlink : 您可以在尝试unlink之前使用is_writable函数:

if( ! @is_writable( "$directory/$file" ) ) {
     echo "works not";
}
else {
    unlink( "$directory/$file" );
    echo "works";
}

is_writable function will show warning (if error is displayed according to configuration) if it fails, so use @ to suppress the error as shown in the CODE above. 如果失败, is_writable函数将显示警告(如果根据配置显示错误),请使用@抑制错误,如上面的CODE所示。

Having said that, your CODE is not wrong either , even with your CODE along with the Error, you should've got works not as output as well. 说了这么多, 你的代码是没有错或者 ,即使与Error沿着你的代码,你应该已经有了works not作为输出为好。 If you don't get it, that most likely mean in your CODE an error handler is set somewhere & the handler is ending the execution by exit or die . 如果您不理解,那很可能意味着在您的CODE中某个地方设置了错误处理程序,并且该处理程序通过exitdie结束执行。

For example, the following CODE will only generate Error, no output (if "$directory/$file" is not writable): 例如,以下代码将仅生成错误,不生成任何输出(如果"$directory/$file"不可写):

function handle_error( $errno, $errstr, $errfile, $errline ) {
    die( $errstr );
}
set_error_handler( 'handle_error' );

// Note: since error handler ends execution with die
// even suppressing warning with @unlink will not give you
// any output other than the error
if( ! @unlink( "$directory/$file" ) ) {
     echo "works not";
}
else {
     echo "works";
}

So check for either set_error_handler or set_exception_handler , see if they are stopping the execution. 因此,请检查set_error_handlerset_exception_handler ,看看它们是否正在停止执行。 Output buffering may also be the cause. 输出缓冲也可能是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM