简体   繁体   中英

unlink doesn't work … file is writable and exist

I have an image sharing website and recently I've noticed I can't delete some of my images via script.

My file is writable and exist (so it's not a permission issue), but why can't I unlink it?

echo    $file = base_path('./files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg');
echo    is_writable($file) ? ' @is_writable ' : ' !!is_writable ';
echo    is_file($file)     ?   ' @is_file ' : ' !!is_file';
$res = unlink($file);
var_dump($res);

Here is the result:

    ./home/siteecom/domains/site.com/public_html/files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg 
    @is_writable @is_file bool(false) 

I've also tried relative path ... didn't work

You probably missed that in order to delete a file you'll need write permissions to the file and it's ancestor folder. Make sure that the directory ./files/images/2013/11 is writable by PHP.

I recently run into an issue like this before. Firstly you'll need to turn on error reporting as unlink() will tell you exactly what's wrong:

ini_set('display_errors', 1);
error_reporting(E_ALL);

You'll want to make sure the directory that contains the file you want to delete is writable ( please supply the chmod permissions for us to help furthur ).

You should look into using realpath() to get the absolute path to the file. (I don't think this is the issue as it doesn't throw a file not found error).


Your problem is almost certainly related to incorrect permissions for the directory you're trying to delete in and also the script thats trying to delete said files.

If you could supply those permissions of both with something like:

echo "Directory = ".substr(sprintf('%o', fileperms(DIRECTORY)), -4) . "<br />";
echo "PHP File = ".substr(sprintf('%o', fileperms(SCRIPT)), -4) . "<br />";

We can try and help you further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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