0% found this document useful (0 votes)
36 views

PHP Tizag Tutorial-64

The document discusses how to delete files in PHP using the unlink function. It explains that unlink removes the connection between a file and the directory it is in, effectively deleting it. It cautions that files must be closed before unlinking and recommends double checking that the correct file is being deleted for safety.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

PHP Tizag Tutorial-64

The document discusses how to delete files in PHP using the unlink function. It explains that unlink removes the connection between a file and the directory it is in, effectively deleting it. It cautions that files must be closed before unlinking and recommends double checking that the correct file is being deleted for safety.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - File Delete

You know how to create a file. You know how to open a file in an assortment of different ways. You even
know how to read and write data from a file!
Now it's time to learn how to destroy (delete) files. In PHP you delete files by calling the unlink function.

PHP - File Unlink

When you view 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. You can think of these filenames
as links that join the files to the directory you are currently viewing.
If you unlink a file, you are effectively causing the system to forget about it or delete it!
Before you can delete (unlink) a file, you must first be sure that it is not open in your program. Use the
fclose function to close down an open file.

PHP - Unlink Function

Remember from the PHP File Create lesson that we created a file named testFile.txt.

PHP Code:
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);

Now to delete testFile.txt we simply run a PHP script that is located in the same directory. Unlink just
needs to know the name of the file to start working its destructive magic.

PHP Code:
$myFile = "testFile.txt";
unlink($myFile);

The testFile.txt should now be removed.

PHP - Unlink: Safety First!

With great power comes a slough of potential things you can mess up! When you are performing the
unlink function be sure that you are deleting the right file!

You might also like