Unlink usage of file deletion function in PHP

Source: Internet
Author: User

Usage

The Nlink () function deletes the file.

If successful, returns True, and returns False if it fails.

Grammar

Unlink (Filename,context)

Example:

The code is as follows Copy Code

$tmpdata = $this->_agency_mod->find (intval ($value));
$img = $tmpdata [$value] [' agency_image '];
if (!empty ($img)) {
$img = '. /data/agency/'. $img;
if (file_exists ($img)) {
Unlink ($IMG);
}
}

First find the entry, query the contents of the entry, including the picture link address. With the address, we use the unlink function to delete the picture file according to the image path. Note: The directory for this picture path should have read and write permissions.

PHP Official Example:

The code is as follows Copy Code

<?php
$fh = fopen (' test.html ', ' a ');
Fwrite ($fh, ' Fclose ($FH);

Unlink (' test.html ');
?>

W3school Example:

The code is as follows Copy Code

<?php
$file = "Test.txt";
if (!unlink ($file))
{
Echo ("Error deleting $file");
}
Else
{
Echo ("Deleted $file");
}
?>

Failed to delete file with Chinese name

Warning:unlink (user_logo/d4d84f383714d5f3.jpg) [Function.unlink]: No such file or directory in

The filename is named in Chinese; the Is_file () is used to detect false. But the file is there. I don't know how to use unlink to delete this file. When you include a file with include, you cannot read the file (the file is not empty).

The code is as follows Copy Code

if (file_exists ($FN)) {//exists the file

Todo Delete

}

If it's Chinese, it might be a problem.

It is now up to is_file or unlink to identify the files in Chinese in the file name. As long as you can identify it can be deleted.

Should be the problem of file name encoding, you can try unlink (iconv (' gb2312 ', ' utf-8 ', ' new text document. txt '));

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.