因项目数据需求,需要对图片文件进行整理和打包
图片存储路径:
/upload/data/img/jpeg/201701/20170100320030303.jpeg
/upload/data/img/jpeg/201702/2017010031030303_small.jpeg
/upload/data/img/jpeg/201703/20170100302430303.jpeg
/upload/data/img/png/201701/20170100303030303.png
/upload/data/img/png/201702/20170100330030303_small.png
/upload/data/img/png/201703/20170100340030303.png
/upload/data/img/bmp/201701/20170100340030303.bmp
/upload/data/img/bmp/201702/20170100360030303_small.bmp
/upload/data/img/bmp/201703/20170100303030303.bmp
包括原图和缩略图
需要把缩略图删掉
然后我就想怎么删掉缩略图,突然觉得自己不会,我就想到了一个办法,把图片挪出来,重新copy一份,然后我新建了个文件夹,把符合要求的原图扔进去,就ok
//遍历文件夹删除文件
public function getDel(){
set_time_limit(0); //放开超时限制,
@ini_set('memory_limit','2048M');//放大内存限制,防止数据量较大时无法导出
try {
$sPath = "upload/data";
$roo_path = ROOT_DIR.'/'.$sPath.'/';
$result = self::my_scandir($roo_path);//读取本地文件列表
foreach($result as $type=>$val){
$vals = array_values($val);
foreach($vals as $k=>$paths){
foreach($paths as $key=>$path){
$akey = explode('/',$path);
$sname = $akey['5'];
$akeys = explode('.',$sname);
if(strpos($akeys[0] , '_500_500')==false){
self::moveimg($path,$sname);
}
}
}
}
} catch (Exception $e) {
echo $e->getMessage();//记录错误log
}
}
function moveimg($path,$sFileName){
$sPath = "upload/allimg/";
$sRealPath = ROOT_DIR.'/'.$sPath;
mkdirs($sRealPath);
$filepath = $sRealPath."/".$sFileName;
$res = file_get_contents($path);
$res2 = file_put_contents($filepath,$res);
}
function my_scandir($dir)
{
$files = array();
if(is_dir($dir))
{
if($handle=opendir($dir))
{
while(($file=readdir($handle))!==false)
{
if($file!="." && $file!="..")
{
if(is_dir($dir.$file))
{
if($file == 'dbback'){//数据库的备份单独处理,不在此处更新
continue;
}
$files[$file]=self::my_scandir($dir.$file."/");
}
else
{
$files[]=$dir.$file;
}
}
}
closedir($handle);
return $files;
}
}
}