本程序的功能是读取目录下的txt 文件,然后保存到数据哦,就这么简单,下面我们就来看看我是如何把txt文件内容保存到mysql数据表的吧。
<?php
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."<br/>");
}
}
else
{
echo($dir_path."<br/>");
}
}
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<br>';
}
else
{
echo mysql_error();
}
}
function openFile($filePath,$title)//读取文件内容提要
{
if( is_file( $filePath ) )
{
$content = file_get_contents( $filePath );
if( !empty( $content ) )
{
savaDate($title,$content);
}
else
{
;
}
}
}
?>