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 '));