PHP – Deleting File

Posted by tutor | Posted in PHP Tutorial | Posted on 09-08-2010-05-2008

0

If there could be a need to create file then definitely you would also require to delete the files.

Now, when we delete a file we actually don’t wash out the space acquired by that file,
rather we make the system forget about the file.
In PHP you can delete files by calling the unlink function.

PHP – File Unlink

Operating System treats a filename as a link which joins a file to a directory.

When you try to see the contents of a directory you can see all the files that exist in that directory
because the operating system or application that you are using displays a list of filenames.

When you unlink a file, you cause the system to forget about it or delete it!

Before deleting a file you must close it by fclose() function.

Unlink() function takes only one argument, which is the name of the file to be deleted.

    For eg:
    $sampleFile = “sample.txt”;
    unlink($sampleFile);

The sample.txt should now be removed. You must make sure that the file exists in the same
directory as the file which is used to delete it.

Although you need to delete files at times but you must take care while deleting files,
as you may delete some other important file also.

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.