首页 > 编程技术 > php

php 删除文件夹(可删除不是空的文件夹)

发布时间:2016-11-25 15:53

这是一款删除文件夹的php函数,本函数用的是递归删除,他可以删除指定目录的所有文件夹与文件,可以删除不是空目录的文件夹,方便的很,下面来看看函数实例方法。
 代码如下 复制代码
/*—————————————————— */
//– 递归删除文件及目录
//– 例: del_dir ('../www.1111cn.net/');注意:返回的/是必须的
//– $type 强制删除目录, true 是 ,false 否
/*—————————————————— */
function del_dir ($dir,$type=true)
{
$n=0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//.svn 忽略 svn 版本控制信息
if ( $file == '.' or $file =='..' or $file == '.svn')
{
continue;
}
if (is_file ($dir.$file))
{
unlink($dir.$file);
$n++;
}
if (is_dir ($dir.$file))
{
del_dir ($dir.$file.'/');
if ($type)
{
$n++;
rmdir($dir.$file.'/');
}
}
}
}
closedir($dh);
}
return $n;
}

 很容易看懂吧。原理很简单,首先是查询目录所有文件或目录,如果是文件就删除了,如果是目录看看是不是空目录,如果是的删除,否则再调用本函数一次类推,就实例了删除不是空目录的功能了。

 代码如下 复制代码
$uid = $_request['uid'];
checkusername($uid);
function checkusername()
{
 $title = $uid;
 if( empty( $title ) )
 {
  return false;
 }
 else
 {
  mysql教程_connect('localhost','root','root');
  mysql_select_db('test');
  mysql_query("set names 'gb2312'");
  $sql = "select * from cn_user where username ='$title'";
  
  $row = mysql_query($sql);
  
  if( mysql_num_rows( $row ) )
  {
   echo '用户名己经存';
  }
  else
  {
   return '可以注册';
  }
 }
}

/*
create table `test`.`cn_user` (
`id` int not null auto_increment ,
`username` varchar( 20 ) not null ,
`times` date null ,
primary key ( `id` )
) engine = myisam

插入数据

insert into `test`.`cn_user` (
`id` ,
`username` ,
`times`
)
values (
null , 'jimmy', null
), (
null , 'www.111cn.net', null
);

*/

本文章是一个实例,功能是数组转换字符串php代码哦,他能把一维数组,二维数组都转换成字符串代码。
 代码如下 复制代码
<?php
$fruits = array (
"fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
$arr1 = array(1, 2, 3, 4, 5, 6=>'fanglor');
function arr2str ($arr)
{
static $res_arr = array();
if (is_array ($arr))
{
foreach ($arr as $key => $val )
{
if (is_array($val))
{
arr2str ($val);
}
else
{
$res_arr[] = $val;
}
}
}
elseif (is_string ($arr))
{
$res_arr[] = $arr;
}
return implode(',',$res_arr);
}
$str = arr2str ($arr1);
print_r ($str);
?>
这里提供了常用正则表达式哦,这是大家在开发中会用到的常用正则表达式代码哦,喜欢就进来看看哦。

一些常用的正则表达式
"^d+$"  //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$"  //正整数
"^((-d+)|(0+))$"  //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$"  //负整数
"^-?d+$"    //整数
"^d+(.d+)?$"  //非负浮点数(正浮点数 + 0)
"^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
"^((-d+(.d+)?)|(0+(.0+)?))$"  //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
"^(-?d+)(.d+)?$"  //浮点数
"^[a-za-z]+$"  //由26个英文字母组成的字符串
"^[a-z]+$"  //由26个英文字母的大写组成的字符串
"^[a-z]+$"  //由26个英文字母的小写组成的字符串
"^[a-za-z0-9]+$"  //由数字和26个英文字母组成的字符串
"^w+$"  //由数字、26个英文字母或者下划线组成的字符串
"^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$"    //email地址
"^[a-za-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?s*)?$"  //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-za-z]{2,4}|[0-9]{1,3})(]?)$" //emil
"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //电话号码
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //ip地址
匹配中文字符的正则表达式: [u4e00-u9fa5]
匹配双字节字符(包括汉字在内):[^x00-xff]
匹配空行的正则表达式:n[s| ]*r
匹配html标记的正则表达式:/<(.*)>.*</1>|<(.*) />/
匹配首尾空格的正则表达式:(^s*)|(s*$)
匹配email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
匹配网址url的正则表达式:^[a-za-z]+://(://w+(-//w+)*)(//.(//w+(-//w+)*))*(//?\s*)?$
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-za-z][a-za-z0-9_]{4,15}$
匹配国内电话号码:(d{3}-|d{4}-)?(d{8}|d{7})?
匹配腾讯qq号:^[1-9]*[1-9][0-9]*$

本款删除数据代码是一款根据用户url地址栏传过来的参数,再去mysql数据库中执行删除操作哦。
 代码如下 复制代码

$host = 'localhost';
$user_name = 'root';
$password = 'admin';

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die('数据库连接失败:'.mysql_error());
}
mysql_select_db('test');

$sql = 'select id,name,city,created_time from users';

$result = mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>产生问题的sql:".$sql);
?>
<html>
<head>
<title>13-12.php</title>
<script language="网页特效">

</script>
</head>
<center>

<body>
<table width="75%" border="0" cellpadding="0" cellspacing="1" bgcolor="#7b7b84">
    <tr bgcolor="#8bbcc7">
        <td height="33"><div align="center"><strong>用户id</strong></div></td>
        <td><div align="center"><strong>用户名称</strong></div></td>
        <td><div align="center"><strong>来自城市</strong></div></td>
        <td><div align="center"><strong>注册时间</strong></div></td>
        <td><div align="center"><strong>操作</strong></div></td>
    </tr>

<?php
if($num = mysql_num_rows($result))
{
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
?>
    <tr bgcolor="#ffffff">
        <td height="22" align="right"><?php echo $row['id']; ?>&nbsp;</td>
        <td height="22">&nbsp;<?php echo $row['name']; ?>&nbsp;</td>
        <td height="22">&nbsp;<?php echo $row['city']; ?>&nbsp;</td>
        <td height="22">&nbsp;<?php echo $row['created_time']; ?>&nbsp;</td>
        <td height="22">&nbsp;<a onclick="javascript:if(confirm('确定要删除用户信息吗?')) return true; else return false;" href="13-13.php?id=<?php echo $row['id']; ?>">删除</a>&nbsp;</td>
    </tr>
<?php
    }
}
mysql_close($conn);
?>

</table>
</body>
</center>
</html>

 代码如下 复制代码

<?php
if(!isset($_get['id']))
{
    echo '参数错误!';
    exit;
}

$id = $_get['id'];
if(empty($id))
{
    echo '用户id不能为空!';
    exit;
}

$host = 'localhost';
$user_name = 'root';
$password = 'admin';

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die('数据库连接失败:'.mysql_error());
}
mysql_select_db('test');

//先判断是否存在该id的用户
$sql = "select * from users where id=$id";
$result = mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>sql:".$sql);
if(!mysql_num_rows($result))
{
    echo '用户id错误!';
    exit;
}

//删除用户数据
$sql = "delete from users where id=$id";
mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>sql:".$sql);
mysql_close($conn);

echo '数据删除成功,返回<a href="13-12.php">13-12.php</a>查看数据';

标签:[!--infotagslink--]

您可能感兴趣的文章: