You may use is_writable
function before trying to unlink
:
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.
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. 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
.
For example, the following CODE will only generate Error, no output (if "$directory/$file"
is not writable):
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. Output buffering may also be the cause.