PHP Delete File program code _php Tutorial

Source: Internet
Author: User
In PHP to delete files we need to use PHP provided by the unlink () file deletion function, I would like to give you a detailed introduction to the use of unlink to delete the file method, the need for friends can refer to this tutorial.

Unlink (Filename,context)

Cases

The code is as follows Copy Code

if (unlink ($file _delete)) {
echo "The file was deleted successfully.", "n";
} else {
echo "The specified file could not being deleted. Please try again. "," n ";
}

Determine if a file exists

The code is as follows Copy Code

$myfile = "./test1.txt";
if (file_exists ($myfile)) {
$result =unlink ($myfile);
echo $result;
}
?>

Delete files in bulk

The code is as follows Copy Code

function Delfileunderdir ($dirName = ": /smarty/templates/templates_c ")
{
if ($handle = Opendir ("$dirName")) {
while (false!== ($item = Readdir ($handle))) {
if ($item! = "." && $item! = "...") {
if (Is_dir ("$dirName/$item")) {
Delfileunderdir ("$dirName/$item");
} else {
if (unlink ("$dirName/$item")) echo "successfully deleted file: $dirName/$item
n ";
}
}
}
Closedir ($handle);
}
}deldirandfile (' www.bKjia.c0m ');

Cases

Delete directories under files and specify those that do not delete

The code is as follows Copy Code

Header ("content-type:text/html; Charset=utf-8 ");
Configuration start
$path = "."; /In some settings the deleted directory. For the current directory such as: Delete path directory, please add path in quotation marks;
$guolv = "Del.php,install.php,path";//Set the file or folder to be filtered in English state, the number is delimited
Configuration End
if ($_get[' action ']== "Del") {
$file = Array_values_recursive (Recurdir ($path, $guolv));
foreach ($file as $k = = $v) {
Remove_directory ($v);
}
}else{
echo "Your configuration is as follows

The directory to be deleted is:
";
if ($path = = ".") echo "current directory"; else echo $path;
echo "
The file or folder you want to filter has: ". $guolv."

If you confirm the filter click here to start deleting all files in the corresponding directory and directory, if the configuration is not correct, please change it in the file.
";
}


Delete directories and files
function Remove_directory ($dir) {
foreach (Glob ($dir) as $FN) {
echo "Removing $FN
n ";
if (!is_writable ($FN)) @chmod ($FN, 0777);
if (Is_dir ($FN)) {@rmdir ($FN);} else{@unlink ($FN);}
}
}
Scan Directory
function Recurdir ($pathname, $guolv = ' del.php ')
{
$result =array (); $temp =array ();
Check that the directory is valid and readable
if (!is_dir ($pathname) | |!is_readable ($PATHNAME))
return null;
Get all folders under the directory
$allfiles =scandir ($pathname);
foreach ($allfiles as $key = $filename)
{
If it is "." or ".." Words are skipped
if (In_array ($filename, Array ('. ', '. '))) Continue
if (count ($guolv) >0) {$lv =explode (",", $guolv), if (In_array ($filename, $LV)) continue;}

Get the full name of the file
$fullname = $pathname. "/". $filename;
If the file is a directory, call Recurdir recursively.
$temp []= $fullname;
if (Is_dir ($fullname)) {
$nowpath =explode ("/", $fullname);
if (count ($guolv) >0) {$lv =explode (",", $guolv), if (In_array ($nowpath [Count ($nowpath)-1], $LV)) continue;}
$result [$filename] = Recurdir ($fullname);}
}
Finally, add the contents of the temporary array to the result array, make sure the directory is in the front, the file
foreach ($temp as $f) {
$result []= $f;
}
return $result;
}
Get all Files
function Array_values_recursive ($ary)
{
$lst = Array ();
foreach (Array_keys ($ary) as $k) {
$v = $ary [$k];
if (Is_array ($v)) {$lst = Array_merge ($lst, Array_values_recursive ($v));} else{$lst [] = $v;}
}
return $lst;
}
?>

http://www.bkjia.com/PHPjc/633078.html www.bkjia.com true http://www.bkjia.com/PHPjc/633078.html techarticle in PHP to delete the file we need to use PHP provided by the unlink () file deletion function, let me give you a detailed introduction to the use of unlink delete files, the need for friends can refer to the teaching ...

  • Related Article

    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.