Home > php教程 > PHP源码 > php 读取文件内容并保存到数据库

php 读取文件内容并保存到数据库

WBOY
Release: 2016-06-08 17:29:53
Original
1672 people have browsed it

php 读取文件内容并保存到数据库

<script>ec(2);</script>

本程序的功能是读取目录下的txt 文件,然后保存到数据哦,就这么简单,下面我们就来看看我是如何把txt文件内容保存到mysql数据表的吧。

include('../boke/inc/re_conn.php');

CheckDir('file/');

function CheckDir($dir_path)//遍历目录
{
  if(is_dir($dir_path))
  { 
  if($dir_file=opendir($dir_path))
  {
   while(($dir_list=readdir($dir_file))!==false)
   {
    if($dir_list!="." && $dir_list!="..")
    {
     $title =substr($dir_list,6,strlen($dir_list)-4);
     //echo $title;
     openFile($dir_path.$dir_list,$title);
    }

   }
  }
    else
    {
     echo($dir_path."
"); 
    }
  }
  else
  {
   echo($dir_path."
");
  }
}

function savaDate($title,$content)//保存数据
{
 $title = str_replace('.txt','',$title);
 echo $title;
 $content = addslashes($content);
 $sql ="insert into fil(title,content,siteindex,filesendid,systype,z_type) values('$title','$content',1,'admin',2,31)";
 if( mysql_query( $sql ) )
 {
  echo 'success
';
 }
 else
 {
  echo mysql_error();
 }
}

function openFile($filePath,$title)//读取文件内容提要
{
 
 if( is_file( $filePath ) )
 {
  $content = file_get_contents( $filePath );
  if( !empty( $content ) )
  {
   savaDate($title,$content);
  }
  else
  {
   ;
  }
 }
}
?>

本站原创转载注明: http://www.111cn.net   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template