php 实现 从一个目录中 把指定类型文件 重新组装到新的目录
为什么要做这个功能
自己从网上下载了很多网页模板, 全部是压缩包, 里面的文个结构也不一致, 没办法方便的浏览到从网上下载的模板
要实现的功能:
从电脑中直接打开文件夹可以看到缩略图, 这样大概知道模板是怎么样的, 新的目录跟下载的压缩包名一致, 目录里同只存放PSD文件
实现方法, 下载ACDSeePro 5 绿色版的, 可以直接看PSD缩略图, 全中所有压缩包, 单击右键--》解压每个压缩文件到单独的文件夹
接来复制文件由PHP来操作
主要使用函数
iconv — 函数库能够完成各种字符集间的转换
opendir — 打开目录句柄
readdir — 从目录句柄中读取条目
closedir — 关闭目录句柄
mkdir — 新建目录
is_dir — 判断给定文件名是否是一个目录
copy — 拷贝文件
end — 将数组的内部指针指向最后一个单元
index.php
<?php
header('Content-Type:text/html; charset=gb2312');
$oldPath = 'http://www.cnblogs.com/../software/网站模板/电子产品';
$newPath = 'http://www.cnblogs.com/../software/网站模板/view/电子产品';
//解决中文目录问题
$oldPath = iconv("utf-8", "gb2312", $oldPath);
$newPath = iconv("utf-8", "gb2312", $newPath);
move_file($oldPath, $newPath, 1);
/** 根据指定文件夹的第一级目录名, 在新的目录建立相应的目录名
* @param string $oldPath (指定文件目录的路径)
* @param string $newPath (在新的目录建立相应的目录名)
* @return void
*/
function create_floder($oldPath, $newPath) {
if (!is_dir($oldPath)) {
return;
}
$handle = opendir($oldPath);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $oldPath . '/' . $file;
if (is_dir($path2)) {
echo $file . "<br>";
mkdir($newPath . '/' . $file);
}
}
}
closedir($handle);
}
/** 把指定文件夹的第一级文件夹的PSD文件当独获取到新的日录, 过滤其他文件格式及多级文件夹
* @param string $oldPath (指定文件目录的路径)
* @param string $newPath (新的目录路径, 把指定文件目录内容复制到这里来)
* @param boolean $changeNewPath (是否为第一级目录, 多级子目录将被过滤)
* @return void
*/
function move_file($oldPath, $newPath = '', $changeNewPath = true) {
if (!is_dir($oldPath)) {
return;
}
$handle = opendir($oldPath);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $oldPath . '/' . $file;
if (is_dir($path2)) {
if ($changeNewPath) { //文件夹下第一级的目录
mkdir($newPath . '/' . $file); //在新的路径建立这些文件夹
move_file($path2, $newPath . '/' . $file);
} else {
move_file($path2, $newPath);
}
} else {
if (get_ext($file) == 'psd') { //将PSD文件复制到相应目录
copy($path2, $newPath . '/' . $file);
}
}
}
}
closedir($handle);
}
//获取文件扩展名
function get_ext($file_name) {
return end(explode('.', $file_name));
}
?>
前望

浙公网安备 33010602011771号