<html>
<?php
include "../_inc/header.php";
include "../_inc/std_func.php";
?>
<body>
<?php
function CreateFolder($delimiter, $dirStr)
{
if(strlen($delimiter)<1)
$delimiter = "/";
$arr = explode($delimiter,$dirStr);
$cnt = count($arr);
$chkDir="";
for($i=0; $i<$cnt; $i++)
{
if(strlen($chkDir) > 0)
$chkDir = $chkDir.$delimiter;
$chkDir = $chkDir.$arr[$i];
if(($arr[$i] == ".") || ($arr[$i] == ".."))
continue;
if(!file_exists($chkDir))
{
if(!mkdir($chkDir))
return 0;
}
}
return 1;
}
/*
//PHP 4
$dir = "/Work/AsRock/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
print_r($files);
*/
/*
//PHP 5
//A little snippet of code that will remove any files beginning with '.'
//Good for removing '.' and '..' as well as any other hidden files.
//Resulting array is $files
$dir = "/Work/AsRock/";
$files = scandir($dir);
foreach($files as $i => $value) {
if (substr($value, 0, 1) == '.') {
unset($files[$i]);
}
}
--------------------
//recursive callback "scanDirectories" did not work for me. So I changed it to:
function scanDirectories($rootDir, $allowext, &$allData = array()) {
$dirContent = scandir($rootDir);
foreach($dirContent as $key => $content) {
if ($content == '.' || $content == '..') continue;
$path = $rootDir . DIRECTORY_SEPARATOR . $content;
$ext = substr($content, strrpos($content, '.') + 1);
if(in_array($ext, $allowext) && is_file($path) && is_readable($path)) {
$allData[] = $path;
}
else if(is_dir($path) && is_readable($path)) {
// recursive callback to open new directory
scanDirectories($path, $allowext, $allData);
}
// else unreadable hell
}
return $allData;
}
*/
//읽을 폴더
//$readDir = "../Work/Driver/Util/";
//$readDir = $_SERVER['DOCUMENT_ROOT']."/Work/AsRock/"; //편한대로 쓰면 된다.
//$readDir = "F:\\다운로드\\AsRock";
$readDir = "E:\\심심풀이\\소설창고\\_PBK\\";
//파일이 저장될 폴더
//$save_dir = $PATH_DRIVER.$dClass."/";
//$save_dir=GetSystemPath("Driver").$dClass."/";
$save_dir = "E:\\심심풀이\\소설창고\\_PBK\\";
$filesInDir = scandir($readDir);
foreach($filesInDir as $i => $fileName)
{
if (substr($fileName, 0, 1) == '.')
{
//unset($filesInDir[$i]);
continue;
}
//폴더 없으면 생성
if(!file_exists($save_dir))
{
CreateFolder("/",$save_dir);
}
$saveName = $fileName;
//원본 경로와 파일명
$fileFrom = $readDir.$fileName;
//////////////파일 이름에서 구분자("-") 왼쪽 오른쪽 나누기
if(0)
{
$file_head = substr($saveName,0,strrpos($saveName,"."));
$file_ext = substr($saveName,strrpos($saveName,".")+1);
$str = "-";
$head_left = substr($file_head,0,strrpos($file_head,$str));
$head_right = substr($file_head,strrpos($file_head,$str)+1);
// +++++ 좌우 변경
//$file_head = ltrim($head_right).$str.rtrim($head_left);
// +++++ 좌측 변경
$file_head = "[".ltrim($head_left)."]".rtrim($head_right);
$saveName = $file_head.".".$file_ext;
}
//+++++++++++++++++++++파일 이름 변경
$saveName = str_replace("] ","]",$saveName);
//변경할 경로와 파일명
$fileTo = $save_dir.$saveName;
//새로 저장할 폴더에 동일한 파일명 존재여부 확인
if(file_exists($fileTo))
{
$file_head = substr($saveName,0,strrpos($saveName,"."));
$file_ext = substr($saveName,strrpos($saveName,".")+1);
$saveName=$file_head.time().".".$file_ext;
$fileTo = $save_dir.$saveName;
}
echo($i."From : ".$fileFrom." ------> To : ".$fileTo."<br>");
//파일 이동
if(!rename($fileFrom,$fileTo))
echo("파일 이동 실패<br>");
}
?>
</body>
</html>
'programmer > web script' 카테고리의 다른 글
[php]폴더 생성(상위 폴더 자동 생성) (1) | 2011.08.01 |
---|---|
[java script]Scroll menu(스크롤바 따라다니는 메뉴) (0) | 2011.08.01 |
[java script]그라데이션(Gradation) 처리 (0) | 2011.08.01 |
[php]zip(압축) 활용 (0) | 2011.08.01 |
apache & jsp & php & mysql 설치 및 설정 (0) | 2011.08.01 |