Search code examples
phphtmllinuxunlink

unlink() is not working


  for($i=0; $i<count($_POST['list']); $i++) { 
            echo $_POST['list'][$i]; 
            if(is_file($_POST['list'][$i])) echo "ok"; else echo "false";
            unlink($_POST['list'][$i]);
       }

I am trying to delete image file in my server.

echo $_POST['list'][$i] outputs ../Profile/JPN012/test2.JPG(route of image).

BUT is_file() and unlink() are not working. Though I changed permission of all directory and image file(0777), it doesn't work.

(Linux OS environment)

What's the problem??


Solution

  • I think the problem is your file path argument in is_file and unlink. If you can change directory to where the file is located before call is_file or unlink, it would be easier. After that, you can use only filename like is_file('test2.png').

       <?php
        $old = getcwd(); // Save the current directory
        chdir($path_to_file);
        unlink($filename);
        chdir($old); // Restore the old working directory    
       ?>
    

    (source: https://www.php.net/chdir)