The php unlink function deletes a file program code.

Source: Internet
Author: User

In php, we can use the unlink function to delete a specified file. The following describes how to delete a specified file by the unlink function, for more information, see.

The unlink () function deletes a file.

If the call succeeds, true is returned. If the call fails, false is returned.

The Code is as follows: Copy code

<? Php
Unlink ("test.txt ");
?>

Example

The Code is as follows: Copy code

<? Php
$ File = "test.txt"; if (! Unlink ($ file )){
Echo ("Error deleting $ file");} else {
Echo ("Deleted $ file ");}
?>

Add judgment

The Code is as follows: Copy code

<? Php
$ File = "test.txt ";
If (unlink ($ file ))
{
Echo "file $ file deleted successfully! ";
}
Else
{
Echo "file $ file deletion failed! ";
}
?>
</Body>
</Html>


Output result:

The file test.txt is deleted successfully!


The following code checks whether a file exists:

The Code is as follows: Copy code

<? Php

$ Myfile = "./test1.txt ";
If (file_exists ($ myfile )){
$ Result = unlink ($ myfile );
Echo $ result;
}
?>

The rmdir () function deletes a directory and returns TRUE if the directory is successfully deleted. Otherwise, FALSE is returned.

Syntax:

Bool rmdir (string dirname)

Example:

The Code is as follows: Copy code

<? Php
$ Dirname = 'testdir ';
If (rmdir ($ dirname )){
Echo "directory $ dirname deleted successfully ";
} Else {
Echo "directory $ dirname deletion failed ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.