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 handler开发者_运维技巧s open. ....what the heck...
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?
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";
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 );
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;
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 ;) ).
file_exists
caches the result of the operation, you should call clearstatcache() before the second call
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-data
in 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
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.
精彩评论