0

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?

1
  • You need to ensure that you have the right permissions for both the directory and the file to be able to delete it. Commented Feb 10, 2012 at 8:17

4 Answers 4

6

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 .

Sign up to request clarification or add additional context in comments.

Comments

1

You do not have permission to delete this file, this file can not be deleted.You can try to modify file permissions.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.