5

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...

8 Answers 8

6

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?

Sign up to request clarification or add additional context in comments.

4 Comments

drwxr-xr-x 15 root root 4096 Aug 19 14:55 . drwxr-xr-x 24 root root 4096 Jul 17 15:28 .. -rwxrwxrwx 1 root root 124991 Aug 19 13:53 file_in_question
That is the permissions for the file I'm trying to delete. Do you want the permissions for the php file?
Nope, that's what I meant.. The file permissions look ok, they can't be causing the problem. What happens if you echo the $log_path from PHP, and try to remove it manually from the command line?
HEY THANK YOU VERY MUCH YOU PUT ME ON THE RIGHT TRACK. I did a chmod 777 . to the directory of the file I was trying to delete. Thank you sir.
4

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";

2 Comments

wow, that's a great line of code. It returns The file was not deleted
@Rell3oT $deleted is actually false. But when used in concatenation, it is converted to empty string, not to 0. That's why in your new output there is nothing after Deleted:
2

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 );

Comments

1

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;

1 Comment

:-) thank you, helped me a lot!
1

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 ;) ).

2 Comments

I copy and pasted your code in (except the setting of $log_file so from $log_path_down. This is my output /path/to/fileTesting: /path/to/file exists? yes deleted? no exists? yes`
where does the initial /path/to/file before testing come from? Are you on a linux or windows system? what does the output of var_dump(decoct(fileperms($log_path))); give you?
0

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

1 Comment

This made no change. Good tip though.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.