简体   繁体   English

如何从目录中删除PHP中的文件

[英]how to delete a file in php from a directory

I am logged into Windows with an administrator account. 我已使用管理员帐户登录Windows。

I used the unlink($filename) function to delete a file using php but it gives me the following error: 我使用unlink($filename)函数使用php删除文件,但它给了我以下错误:

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 警告:取消链接(C:/ wamp / www / jedda / wp-content / uploads /)[function.unlink]:权限在C:\\ wamp \\ www \\ Jedda \\ wp-content \\ plugins \\ course management \\ course_file.php中被拒绝在第242行

So how can I delete the file using php? 那么如何使用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 . Unlink只能删除文件NOT文件夹。 Make sure your argument to unlink() is a valid file . 确保您对unlink()的参数是有效文件。

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

If you want to delete a directory you have to use rmdir command. 如果要删除目录,则必须使用rmdir命令。 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 . 您可以先使用诸如scandir之类的功能列出特定目录的文件和目录,然后使用unlink删除文件。

Use this PHP script:- 使用此PHP脚本:-

<?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 该脚本将删除文件或文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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