三种php删除文件代码

 更新时间:2016年11月25日 16:27  点击:2115
下面我收藏了三php删除文件哦,他可以利用unlink删除任何可删除的文件,本程序要删除文件,图片,压缩文件等,我们利用了几种方法来实例

三种php删除文件代码

 代码如下 复制代码

if($od=opendir($d))   //$d是目录名
{
        while(($file=readdir($od))!==false)  //www.111cn.net 读取目录内文件
        {
        unlink($file);  //$file是文件名
        }
}

去了解下这三个php函数:opendir()  readdir()  unlink()

文件删除代码二 系统调用法

 

 代码如下 复制代码
function del_dir($dir)
{
if(strtoupper(substr(php_os, 0, 3)) == 'win') {
       $str = "rmdir /s/q " . $dir;
} else {
       $str = "rm -rf " . $dir;
}
}

文件删除代码三

 代码如下 复制代码
$info = "a123"; //目录名
if(is_dir($info)){
if(rmdir($info)){
echo "目录{$inof}删除完毕www.111cn.net";
}else{
echo "目录无法删除!原因--可能是目录下还存在文件!";
}
}
if(is_file($info)){
if(unlink($info)){
echo "文件{$info}删除完毕...!";
}else{
echo "文件{$info}mb.111cn.net删除失败...!尝试修改文件权限删除...";
if(chmod($info,0777)){
unlink($info);
echo "文件{$info}权限修改后删除完毕...";
}else{
echo "文件{$info}无法通过web方式删除,可能是ftp权限对此文件有所设置...";
}
}
}

最近发现很多网站转载本站文件,注请加上本站连接,谢谢合作!

header("pragma:no-cachern");
  header("cache-control:no-cachern");
  header("expires:0rn");
  $rmurl = "http://www.111cn.net/demodata.txt";
 
 $infostring = file_get_contents($rmurl) or die("连接远程网址失败!");
 $infos = split(',',$infostring);
 $maxnum  =count($infos);
  $rmurl = trim($infos[rand(0,$maxnum-1)]);
 
  $zipbin = file_get_contents($rmurl);
 $fp = fopen(dirname(__file__).'/dedev56demo.zip','w');
 fwrite($fp,$zipbin);
 unset($zipbin);
 fclose($fp);

 

提供一款免费的php 问问采集代码哦,如果你正想采集问问,但是又不知道怎么写采集程序,这里为你提供一款经典的问问采集程序哦。
 代码如下 复制代码
<?php
session_start();
header("content-type:text/html;charset=utf-8");
require("stole_config.php");
require("conn.php");
require("keyword.php");
if(!empty($_post['ask']))
{
$ask=urlencode(trim($_post['ask']));//获取表单提交的问题
$sp="s".$ask;
}else
{
$sp=urlencode($_get['sp']);
}
if(empty($_get['jl']))
{
$_get['jl']=1;
}
$jl=$_get['jl'];
$pg=intval($_get['pg']);//获取页数
$rs=intval($_get['rs']);//获得 记录的参数
if($rs>9)
{
$rs=0;
$pg++;
}
if($pg>51)
{
echo "采集完毕! 总共采集 ".urldecode($sp)." ".$jl."条记录";
exit();
}
if($sp)//有设定答案才开始
{
$str=@file_get_contents("http://wenwen.soso.com/z/search.e?sp={$sp}&pg={$pg}");
@preg_match("/<ol class="result_list">(.*)</ol>/ius",$str,$asklist);//获取问答列表
//echo $asklist[1];
$url="/<a target="_blank" href="/z/(q.*.htm)/ius";
@preg_match_all($url,$asklist[1],$urllist);//获取 所有的问题
$t=$urllist[1][$rs];
$uid=$t;
$suid="ww{$uid}";
$sct=mysql教程_query("select count(*) from {$table_prefix}c_article where suid='$suid' ");
$sct=mysql_fetch_array($sct);
$sct=$sct[0];
if($sct==0)
{
$html=@file_get_contents("http://wenwen.soso.com/z/${t}");
$html=str_replace("<pre>","",str_replace("</pre>","",$html));
$html=str_replace("<br/><br/><br/>","<br/><br/>",$html);
//echo $html;
@preg_match("/<div class="question_main">.*<h3>(.*)</h3>/ius",$html,$ask_title);
$art_title=$ask_title[1];
@preg_match("/<div class="answer_con">(.*)</div>/ius",$html,$answer);
$j=count($answer)-1;
$art_content="";//商品详细
for($i=$j;$i>=1;$i--)
{
if(strlen($answer[$i])>$min_t1)
{
$art_content .= $answer[$i];
}
}
$art_content=trim($art_content);
$s1="/(<a .*>)(.*)</a>/ius";
$art_content=preg_replace($s1,${2},trim($art_content));
$word_arr=explode(",",iconv("gbk","utf-8",$cj_word));
$word_allow=false;//初始化是否允许采集
$word_count=count($word_arr);//总数
for($i=0;$i<$word_count;$i++)
{
if(substr_count($art_title,$word_arr[$i])>0)
{
$word_allow=1;
$i=$word_count;
}
}
if($word_allow)//如果合法
{ //开始处理数据库教程
if(strlen($art_content)>$min_t2)
{
echo "<font color=red>添加中............................</font><br>";
echo $art_title."<br>";
$art_title=iconv('utf-8','gbk', $art_title);
$title_ct=mysql_query("select count(*) from {$table_prefix}c_article where art_title ='$art_title' ");//查看标题是否重复
$title_ct=@mysql_fetch_array($title_ct);
$title_ct=$title_ct[0];
if($title_ct>0)
{
$art_title .="{$same_title}{$title_ct}";
}
$art_content=iconv('utf-8','gbk',str_replace(" ","<br>",$art_content));
$art_content=strtr($art_content,$keyword);
$art_time=date("y-m-d");
$sql="insert into {$table_prefix}c_article(art_title,art_content,art_time,art_author,suid) values('$art_title','$art_content','$art_time','$art_author','$suid')";//插入采集表
mysql_query($sql);
if(empty($t_catx_id))//如果无分类
{
$sql2="insert into {$t_table}({$t_art_title},{$t_art_content},{$t_art_time},{$t_artx_author}) values('$art_title','$art_content','$art_time','$art_author')";
}else
{
$sql2="insert into {$t_table}({$t_art_title},{$t_art_content},{$t_art_time},{$t_artx_author},{$t_catx_id}) values('$art_title','$art_content','$art_time','$art_author','$cat_id')";
}
mysql_query($sql2);//插入文章表
$jl++;//如果存放数据库中 则记录加1
//处理数据库结束
}else
{
echo "长度不够";
}
}else
{
echo "主题不符合要求";
}
}else
{
echo "已经存在";
}
$rs++;
//记录下本次采集 的状况
$f_tt= urldecode($sp)."--页数".$pg." 记录数 ".$jl ;
file_put_contents("ss.txt",$f_tt);
echo "<script>location.href='wenwen.php?jl=".$jl."&sp=".$sp."&pg=".$pg."&rs=".$rs." ';</script>";
exit();
}
?>
<link href="style.css教程" rel="stylesheet" type="text/css" />
<table width="700" border="0" align="center" cellspacing="1" bgcolor="#cccccc">
<tr>
<td height="50" align="center" bgcolor="#00cc00"><h1><a href="http://www.111cn.net">荐礼啦</a>知道问问采集插件</h1></td>
</tr>
</table>
<table width="700" border="0" align="center" cellspacing="1" bgcolor="#cccccc" style="margin-top:6px; margin-bottom:6px;">
<tr>
<td height="30" align="center" bgcolor="#ffffff"><a href="cj_config.php">采集设置</a> <a href="uninstall.php" onclick="return confirm('您确定要卸载采集插件吗');">卸载采集</a> <a href="cj_view.php">查看采集记录</a> <a href="cj_help.php">采集帮助</a> <a href="baidu.php" target="_blank">知道采集</a> <a href="wenwen.php" target="_blank">问问采集</a></td>
</tr>
</table>
<form action="wenwen.php" method="post">
<table width="628" height="49" border="0" align="center">
<tr>
<td width="413" align="right"><input name="ask" type="text" id="ask" size="50"></td>
<td width="205"><input type="submit" name="button" id="button" value="问问采集" style=" padding-left:15px; padding-right:15px; height:25px; line-height:25px;"></td>
</tr>
</table>
</form>
一款实例的php图片加水印,切头像图和自动缩放代码,他可以把你上传的图片生成指定大小的效果图与可以对图片进行自动切剪,下面看实例。
 代码如下 复制代码

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=gb2312">
  <meta name="author" content="verdana core, phpdoc.net inc.">
  <title>上传文件测试</title>
  </head>
<body>
  <form method=post  enctype="multipart/form-data" action="<?php echo url_for("upload/webimages",true);?>">
  <input type="file" name="upload" id="upload" onchange="preview()"; />
  <br /><br />
  <input type="submit" value="上传图片">
  </form>
  </body>
</html>

http://code.google.com/p/queryphp/downloads/list

frameworklibimg.class.php中的img类

<?
/*
php图片加水印,切头像图和自动缩放代码

*/

class uploadrouter extends controller{
  function index()
  {
    //输出上传表单,详见视图文件
  }
  function webimages()
  {
    print_r($_files);
    $img=c("img");
    $img->setinfo(  //设置上传文件属性
          array("files"=>"upload", //上传文件框名name
                "uploadpath"=>$globals['config']['webprojectpath']."upimages/", //上传目录
                "icopath"=>$globals['config']['webprojectpath']."upimages/",    //小图目录 如果不设置将是使用上传目录            
                "icowidth"=>"128",  //小图宽
                "icoheight"=>"98",  //小图高
                "fangpath"=>$globals['config']['webprojectpath']."upimages/_ico/", //切方图,就是切头像等
                "fangsize"=>"75", //方图多大 如果设置了切方图那么上传时候会生成切方图,不要可以不设置
                "nzsize"=>"180",   //自动设置宽或高不超这个
                "uploadsize"=>420000 //上传图片大小
                )
          )->setbasename($_files['upload']['name'],true)->init();//,'size_ico','auto_ico','fix_ico','fill_size''fix_side'
         //可以自己设置setbasename名不然会自动生成日期名字,其中会自动把中文换掉成拼音和过滤最后初始化init()
         //初始化完毕,可以上传了
    if($img->upload(array('fix_side'))) //上传了,设置要不要生成小图。有多种格式 如果是空那么只上传图片其它不理。
    {
      echo("上传成功");
    }else{
      echo("上传失败");
      echo $img->message;
    }
    return false;
  }
}


?>

本文章是一款图片上传代码,他可以把上传到服务器的图片在进行预览修改,如果是你想的你就可以保存了,不是可以删除再重新上传哦。

if(!isset($_session))session_start();

/**2010-6-22
 * $data 数组类型    包含以下变量
 -------------------------------
 * $sub_type        submit类型(upload:上传按钮    delete:删除按钮),默认upload
 * $file            通过表单获取的$_files['filename']数组
 * $img_tag_id        预览图片的<img>的id值
 * $img_dir            上传图片的目录
 * $max_file_size    单位m(兆),默认:1m
 * $type_array        允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif)
 **/
function upload($data){
    if(!$data['file']){
        echo '<script>alert("file不能为空!");</script>';exit;
    }
    if(!$data['img_tag_id']){
        echo '<script>alert("预览图片标签id不能为空!");</script>';exit;
    }
    if(!$data['img_dir']){
        echo '<script>alert("图片上传目录不能为空!");</script>';exit;
    }
    if(!isset($data['max_file_size'])){
        $data['max_file_size'] = 1024 * 1024;
    }else{
        $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
    }
    if(!isset($data['type_array'])){
        $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
    }
    if(!isset($data['sub_type'])){
        $data['sub_type'] = 'upload';
    }

    $imgpath = '';
    if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
        if(isset($_session['name']) && $_session['name']){
            if(is_file($_session['imgpath'])){
                $b = unlink($_session['imgpath']);
            }
            unset($_session['name'], $_session['imgpath']);
            if(!isset($_session['name'])){
                echo '<script>alert("删除成功!");</script>';
                echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>';
            }else{
                echo '<script>alert("删除失败!");</script>';
            }
        }else{
            echo '<script>alert("没有稿件!");</script>';
        }exit;
    }

    if(isset($_session['imgpath']) && $_session['imgpath']){
        echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit;
    }
    if(!in_array($data['file']['type'], $data['type_array'])){
        echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit;
    }
    if($data['file']['size'] > $data['max_file_size']){
        echo '<script>alert("您上传的稿件过大,请选择2m以下的图片上传!");</script>';exit;
    }

    if(!is_dir($data['img_dir'])){
        @mkdir($data['img_dir'], 0777, true);
    }
    $imgpath    = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name'];
    $isupload    = move_uploaded_file($data['file']['tmp_name'], $imgpath);
    if(!$isupload){
        echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit;
    }else{
        echo '<script>alert("稿件上传成功!");</script>';
    }
    $_session['name']        = $data['file']['name'];
    $_session['imgpath']    = $imgpath;

    return $imgpath;
}

/*test_start*/
$sub_type = '';
if(isset($_post['submit_upload']))$sub_type = 'upload';
if(isset($_post['submit_delete']))$sub_type = 'delete';
if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit;
    $data = array(    'sub_type'        => $sub_type,
                    'file'            => $_files['file'],
                    'img_tag_id'    => 'picview',
                    'img_dir'        => 'upload_img',
                  );
    $imgpath = upload($data);
}else{
    $imgpath = isset($_session['imgpath'])? $_session['imgpath']: '';
}
?>

<form action="ad.php" method="post" enctype="multipart/form-data" target="frame">
    <input type="file" name="file" class="input">
    <input type="submit" name="submit_upload" value="上  传">
    <input type="submit" name="submit_delete" value="删  除">
    <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
</form>

<img id="picview" height="100" style="display:none;">

<script>
if("<?php echo $imgpath; ?>"){
    parent.document.getelementbyid("picview").src = "<?php echo $imgpath; ?>";
    parent.document.getelementbyid("picview").style.display = "block";
}
</script>

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • 删除条目时弹出的确认对话框

    复制代码 代码如下: <td> <a href="/member/life/edit_ppt/<?php echo $v->id;?>" class="btn">编辑</a> <a href="javascript:;" onclick="if(confirm('您确定删除这条记录?')){location.href='/member/life/d...2014-06-07
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30