昨天做一个QQ号码过滤程序,要求上传一个txt文件,回车分隔,把里面的qq号码上传至数据库已经有的过滤掉,然后所有的qq号码生成一个txt文件,打包好提供下载
<?php
error_reporting(E_ALL || ~E_NOTICE);
header('Content-Type:text/html;charset=GB2312');
date_default_timezone_set('Asia/Shanghai');
define('MUILTI_FILE_UPLOAD', '1'); //
define('MAX_SIZE_FILE_UPLOAD', '5000000' ); //文件大小不超过
define('FILE_UPLOAD_DIR', './'); //上传文件的目录
$cfg_dbhost='127.0.0.1';
$cfg_dbuser='root';
$cfg_dbpwd='root';
$cfg_dbname='test';
$conn=MySQL_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
MySQL_select_db($cfg_dbname, $conn);
MySQL_query("set names gbk;");
//允许上传的文件名
$array_extention_interdite = array( '.txt' ); //上传文件的扩展名
$createtime=filemtime("qq.zip");
$xiazailianjie='<a href="qq.zip">下载链接</a> 创建时间:'.date("Y-m-d H:i:s",$createtime);
//显示信息的公共函数
function func_message($message='', $ok=''){
echo '<table width="100%" cellspacing="0" cellpadding="5">';
if($ok == true){
echo '<tr bgcolor="#99FF99" ><td width="100"> </td><td class= "text"> '.$message.'</td></tr>' ;
}
if($ok == false){
echo '<tr bgcolor="#FF99CC" ><td width="100"> </td><td class="text"> '.$message.'</td></tr>';
}
echo '</table>';
}
//处理表单提交
$action = (isset($_POST['action'])) ? $_POST['action'] :'' ;
$file = (isset($_POST['file'])) ? $_POST['file'] :'' ;
if($file != '') {
$file = $file.'/';
}
$message_true = '';
$message_false = '';
switch($action){
case 'upload' :
chmod(FILE_UPLOAD_DIR,0777);
if( $_FILES['file']['size'] >= 10 ){
if ($_FILES['file']['size'] <= MAX_SIZE_FILE_UPLOAD ){
if (!in_array(ereg_replace('^[[:alnum:]]([-_.]?[[:alnum:]])*\.' ,'.', $_FILES['file']['name'] ) , $array_extention_interdite) ){
if($_POST['file_name'] !=''){
$file_name_final = $_POST['file_name'].$extension ;
}else {
$file_name_final = $_FILES['file']['name'] ;
}
$_FILES['file']['name'] = $file_name_final;
move_uploaded_file( $_FILES['file']['tmp_name'],FILE_UPLOAD_DIR.$file.$file_name_final );
$message_true .= '已经上传文件 : '.$_FILES['file']['name'] .'<br>';
}else{
$message_false .= '文件上传失败 : '.$_FILES['file']['name'] .' <br>';
}
}else{
$message_false .= '文件尺寸超过'.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : " '.$_FILES['file']['tmp_name'].'" <br>';}
}
$file_handle = fopen($_FILES["file"]["name"], "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$line=preg_replace('/\n|\r\n/','',$line);
if($line!=''){
$result = mysql_query("select * from qq where qq ='".$line."'");
if(!mysql_fetch_array($result)){
mysql_query("INSERT INTO `qq` (`qq`) VALUES ('".$line."');");
}
}
}
mysql_free_result($result);
fclose($file_handle);
unlink ($_FILES['file']['name']);
unlink('qq.zip');
createFile('qq.txt');
$result = mysql_query('select qq from qq;');
while($row = mysql_fetch_array($result))
{
totxt($row['qq'],'qq.txt');
}
mysql_free_result($result);
$files=array('qq.txt');
create_zip($files, 'qq.zip', true);
unlink ('qq.txt');
$createtime=filemtime("qq.zip");
$xiazailianjie='<a href="qq.zip">下载链接</a> 创建时间:'.date("Y-m-d H:i:s",$createtime);
}
mysql_close($conn);
?>
<html>
<head>
<title>上传过滤重复QQ号码</title>
<style>
.border{background-color:#000000}
.box{background-color:#f8f8f9;}
.text{ color:#000000;
font-family: 宋体;
font-size: 12px;
font-weight:bold}
input, select{font-size: 12px;}
body {
margin-top: 8px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body marginwidth="0" bottommargin="0" leftmargin="0" rightmargin="0">
<form name="form" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'] ; ?>">
<input type="hidden" name="action" value="upload">
<table border="0" cellspacing="1" cellpadding="0" align="center" class="border">
<tr>
<td>
<table width="100%" border="0" cellspacing="5" cellpadding="2" align="center" class="box">
<tr class="text">
<td>上传文件: </td> <td><input type="file" name="file"></td>
<td></td><td></td>
</tr>
<tr>
<td colspan="2" align="right" class="text">
</td>
<td colspan="2" align="right">
<input type="submit" value="上传分析并获取下载链接">
</td>
</tr>
<tr>
<td style="font-size:12px;"><?php echo $xiazailianjie; ?></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
$zip->close();
return file_exists($destination);
}else{
return false;
}
}
?>
<?php
function totxt($somecontent,$filename){
$somecontent = $somecontent."\r\n";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
print "不能打开文件 $filename ";
exit;
}
if (!fwrite($handle, $somecontent)) {
print "不能写入到文件 $filename ";
exit;
}
fclose($handle);
} else {
print "文件 $filename 不可写 ";
}
}
function createFile($filename){
$phpfile = $filename;
if (! file_exists ( $phpfile )) {
$cFile = fopen ( $phpfile, 'w' );
if (! $cFile) {
echo 'create '.$phpfile.' file fail';
}
fclose($cFile);
}
}
?>