简体   繁体   中英

how to delete a file in php from a directory

I am logged into Windows with an administrator account.

I used the unlink($filename) function to delete a file using php but it gives me the following error:

Warning: unlink(C:/wamp/www/jedda/wp-content/uploads/) [function.unlink]: Permission denied in C:\\wamp\\www\\Jedda\\wp-content\\plugins\\course management\\course_file.php on line 242

So how can I delete the file using php?

See the error :

unlink(C:/wamp/www/jedda/wp-content/uploads/)

You are trying to delete the folder "uploads" , not the file . Unlink can delete files only NOT folder . Make sure your argument to unlink() is a valid file .

您没有删除此文件的权限,无法删除此文件。您可以尝试修改文件权限。

If you want to delete a directory you have to use rmdir command. And the specific directory has to be empty. You can use a function like scandir first to list the files and directories of the specific directory and delete the files with unlink .

Use this PHP script:-

<?php
    function rmdirr($dirname)
    {
        // Sanity check
        if (!file_exists($dirname)) {
            return false;
        }

        // Simple delete for a file
        if (is_file($dirname)) {
            return unlink($dirname);
        }

        // Loop through the folder
        $dir = dir($dirname);
        while (false !== $entry = $dir->read()) {
            // Skip pointers
            if ($entry == '.' || $entry == '..') {
                continue;
            }

            // Recurse
            rmdirr("$dirname/$entry");
        }

        // Clean up
        $dir->close();
        return rmdir($dirname);
    }
?>

This script is to delete a file or a folder.... both

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