user583507
user583507

Reputation:

php unlink. returns no error message and doesn't delete my file

I'm simply trying to delete a file using PHP's unlink. I found an answer on here that told me the best way to do this and I implemented it. Heres the code I am using.

$log_path = realpath($log_file);
if (is_readable($log_path)){
    print file_exists($log_path);
    $deleted = unlink($log_path);
    print "Deleted:".$deleted;
    print "Exists:".file_exists($log_path);
}

This just prints 1Deleted:exists:1. I also tried to add in print_r(error_get_last()); under the unlink, but this also returned nothing. All files in the directory are chmod 777 * and there are no other file handlers open. ....what the heck...

Upvotes: 5

Views: 13652

Answers (8)

Dima Odnokoz
Dima Odnokoz

Reputation: 1

unlink function in my case just returned false everytime. First, thanks to Viktor for this:

Also, add next line to begin of your script to show all errors and warnings: ini_set( 'error_reporting', E_ALL );

It helped me to see warning:

unlink(): http does not allow unlinking in PHP.

And i finally found the solution, described here https://a1websitepro.com/warning-unlink-http-not-allow-unlinking-php/.

The case is that I use http path to the file, but unlink function requires the absolute path to the file from your server.

Upvotes: 0

Salem
Salem

Reputation: 774

for me it was promises issue solve it with

  chown    -R www-data:www-data      /var/www/
  chmod    -R g+rwx                  /var/www/
  chmod    -R 0755                  /var/www/

make sure that php run under user www-datain www.conf e.g I'm using PHP 7.2 nano /etc/php/7.2/fpm/pool.d/www.conf Change these values :-

listen.owner = www-data
listen.group = www-data

Upvotes: 0

Victor
Victor

Reputation: 5769

I think you need to check if $log_path is file. Use:

if( is_file( $log_path ) AND is_readable( $log_path ) )

Also, add next line to begin of your script to show all errors and warnings:

ini_set( 'error_reporting', E_ALL );

Upvotes: 2

Lars
Lars

Reputation: 5799

I don't really know the problem, but you should check, if the file is writable and not if it is readable:

<?php
$log_file = "/tmp/test.log";
$log_path = realpath($log_file);
echo "Testing: ". $log_path."\n";
if (is_writable($log_path)) {
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
    $deleted = unlink($log_path);
    echo "deleted? ";
    echo ($deleted)?"yes\n":"no\n";
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
} else {
    echo "file unwritable\n";
}

This code works fine for me (yes, it's also messy ;) ).

Upvotes: 1

Rijk
Rijk

Reputation: 11301

The code you used is needlessly cumbersome. A simple call to unlink should do the trick:

unlink( $log_file );

But let's find out what is going wrong. The file exists, because you enter the loop where the print statements are done. The unlink call probably returns false, because the output is "11" and not "111".

So my gut says, it must be a file permissions issue. Are you sure the web user has permission to remove the file? Can you show us the folder permissions, for instance by running ls -la on the command line and pasting the output?

Upvotes: 6

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

file_exists caches the result of the operation, you should call clearstatcache() before the second call

Upvotes: 0

Kaken Bok
Kaken Bok

Reputation: 3395

Its not that your files need to be 0777. It also necessary that your directory can be accessed. What's the mod of your directory?

Next: print $deleted; apparently print false, which is shown as nothing. Try this: echo $deleted ? 1 : 0;

Upvotes: 1

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99911

unlink returns either true or false. If you try to print false (print $deleted;), this will print an empty string.

Try this instead:

print $deleted ? "The file was deleted" : "The file was not deleted";

Upvotes: 4

Related Questions