本文实例讲述了php导入文件夹图片保存到dedecms的方法。分享给大家供大家参考。具体分析如下:
数据库连接这里就不介绍了,这里就是简单的mysql连接,我们主要是看核心代码,原理就是glob函数遍历目录,然后再利用foreach遍历读取一个个的文件名字再由相关函数处理之后返回给我们编译好的sql语句再执行三表操作即可.
临时写的一个东西省时间而写的,因为有几千个产品带大图,所以我们写了这么一个程序了,希望对各位有帮助.
导入方法:http://localhost/a.php?typeid=你的id&path=你的目录
处理php文件,代码如下:
$typeid = $_GET['typeid'];
if( ! intval( $typeid ) )
{
echo '请传值给typeid';
exit;
}
$path = $_GET['path'];
if( $path =="" )
{
echo '请传值给path ,如 a/b/c/';
exit;
} </p> <p>function getfile($path)
{
$dirs = array();
foreach(glob("$path*") as $d)
{
$tmp = explode('.',$d);
$k = end($tmp);
if(is_file($d) && ( $k=='jpg' || $k =='gif' || $k=='png'))
{
$dirs[] = $d;
}
}
return $dirs;
}
$array = getfile($path);
foreach( $array as $v => $vv )
{
//取名字
$t_name = explode( '/',$vv ); ;
$tV = end($t_name);
list($name,$ext) = explode('.',$tV);
$sql ="INSERT INTO `dedecms_archives` (`typeid`, `typeid2`, `sortrank`, `flag`, `ismake`, `channel`, `arcrank`, `click`, `money`, `title`, `shorttitle`, `color`, `writer`, `source`, `litpic`, `pubdate`, `senddate`, `mid`, `keywords`, `lastpost`, `scores`, `goodpost`, `badpost`, `voteid`, `notpost`, `description`, `filename`, `dutyadmin`, `tackid`, `mtype`, `weight`) VALUES
($typeid, '0', 1394943742, 'c,p', 1, 17, 0, 156, 0, '$name', 'S".$typeid.mt_rand(0,99999)."', '', 'admin', '未知', '/$vv', 1394943742, 1394943720, 1, '$name', 0, 0, 0, 0, 0, 0, '', '', 1, 0, 0, 11)";
mysql_query( $sql ) or die( mysql_error().$sql );
$aid= mysql_insert_id();
$sql2 ="INSERT INTO `dedecms_addonarticle17` (`aid`, `typeid`, `body`, `redirecturl`, `templet`, `userip`, `s_type`) VALUES
($aid, $typeid, '<img src=/".str_replace('231','600',$vv)." />', '', '', '192.168.1.108', 'S12000".$name."')";
mysql_query( $sql2 ) or die( mysql_error().$sql2 );
$sql3 ="INSERT INTO `dedecms_arctiny` (`id`, `typeid`, `typeid2`, `arcrank`, `channel`, `senddate`, `sortrank`, `mid`) VALUES
($aid, $typeid, '0', 0, 17, 1394943720, 1394943742, 1)";
mysql_query( $sql3 ) or die( mysql_error().$sql3 );
}
echo '导入成功';
print_r($array);
if( ! intval( $typeid ) )
{
echo '请传值给typeid';
exit;
}
$path = $_GET['path'];
if( $path =="" )
{
echo '请传值给path ,如 a/b/c/';
exit;
} </p> <p>function getfile($path)
{
$dirs = array();
foreach(glob("$path*") as $d)
{
$tmp = explode('.',$d);
$k = end($tmp);
if(is_file($d) && ( $k=='jpg' || $k =='gif' || $k=='png'))
{
$dirs[] = $d;
}
}
return $dirs;
}
$array = getfile($path);
foreach( $array as $v => $vv )
{
//取名字
$t_name = explode( '/',$vv ); ;
$tV = end($t_name);
list($name,$ext) = explode('.',$tV);
$sql ="INSERT INTO `dedecms_archives` (`typeid`, `typeid2`, `sortrank`, `flag`, `ismake`, `channel`, `arcrank`, `click`, `money`, `title`, `shorttitle`, `color`, `writer`, `source`, `litpic`, `pubdate`, `senddate`, `mid`, `keywords`, `lastpost`, `scores`, `goodpost`, `badpost`, `voteid`, `notpost`, `description`, `filename`, `dutyadmin`, `tackid`, `mtype`, `weight`) VALUES
($typeid, '0', 1394943742, 'c,p', 1, 17, 0, 156, 0, '$name', 'S".$typeid.mt_rand(0,99999)."', '', 'admin', '未知', '/$vv', 1394943742, 1394943720, 1, '$name', 0, 0, 0, 0, 0, 0, '', '', 1, 0, 0, 11)";
mysql_query( $sql ) or die( mysql_error().$sql );
$aid= mysql_insert_id();
$sql2 ="INSERT INTO `dedecms_addonarticle17` (`aid`, `typeid`, `body`, `redirecturl`, `templet`, `userip`, `s_type`) VALUES
($aid, $typeid, '<img src=/".str_replace('231','600',$vv)." />', '', '', '192.168.1.108', 'S12000".$name."')";
mysql_query( $sql2 ) or die( mysql_error().$sql2 );
$sql3 ="INSERT INTO `dedecms_arctiny` (`id`, `typeid`, `typeid2`, `arcrank`, `channel`, `senddate`, `sortrank`, `mid`) VALUES
($aid, $typeid, '0', 0, 17, 1394943720, 1394943742, 1)";
mysql_query( $sql3 ) or die( mysql_error().$sql3 );
}
echo '导入成功';
print_r($array);
之后碰到一个小问题,就是导入数据在栏目可以看到,但在所有数据中看不到记录,生成时只有选择栏目生成才可以但用一键生成有问题,这个时我在发布时间是固定一个以前的时间,对此大家可以使用time函数来获取当前时间.
希望本文所述对大家的dedecms建站有所帮助。