我需要按语言和libraryID对大量书籍进行分类。
文件名的结构如下,并分布在许多文件夹中:
ENG_FILENAME_LIBRARYNAME1.pdf
ENG_FILENAME_LIBRARYNAME2.pdf
SPA_FILENAME_LIBRARYNAME1.pdf
SPA_FILENAME_LIBRARYNAME2.pdf
我需要将它们移到像这样的文件夹中
ENG
->LIBRARYNAME1
--ENG_FILENAME_LIBRARYNAME1.pdf
->LIBRARYNAME2
--ENG_FILENAME_LIBRARYNAME2.pdf
这是我的代码:
foreach (glob("C:/wamp/www/projects/filemove/eth/*") as $folderpath) {
$foldername = preg_replace('/.*?\/(.*?)/', '$1', $folderpath);
foreach (glob("C:/wamp/www/projects/filemove/eth/*/*") as $librarypath) {
$libraryname = preg_replace('/.*?\/(.*?)/', '$1', $librarypath);
foreach (glob("C:/wamp/www/projects/filemove/pdf/*.pdf") as $filepath) {
$ethologue = preg_replace('/(.*?)_.*/', '$1', $filepath);
$library = preg_replace('/.*?_.*?_.*?_(.*?)_.*/', '$1', $filepath);
$filename = preg_replace('/.*?\/(.*?)/', '$1', $filepath);
if ($ethologue = $foldername ) {
if ($library = $libraryname) {
copy($filepath, $librarypath);
}
}
}
}
}
提前致谢!
答案 0 :(得分:1)
我唯一能看到的就是你在if语句中做作业。这是故意的吗?
if ($ethologue = $foldername ) {
if ($library = $libraryname) {
这应该是一个比较:
if ($ethologue == $foldername ) {
if ($library == $libraryname) {
答案 1 :(得分:1)
你可以尝试一下
define("FILE_COPY", 1);
define("FILE_MOVE", 2);
$dirSource = __DIR__ . "/case";
$dirDestination = $dirSource . "/books";
$cache = $errors = array();
$allowDuplicate = false;
$mode = FILE_COPY; // copy or move file ;
try {
$dirIT = new FilesystemIterator($dirSource, FilesystemIterator::SKIP_DOTS);
$regexIT = new RegexIterator($dirIT, '/.pdf$/i');
// Make Directory
if (! is_dir($dirDestination) and ! @mkdir($dirDestination, 0777, true)) {
throw new Exception("Destination Folder ($dirDestination) does not exist");
}
foreach($regexIT as $splFile) {
$hash = md5_file($splFile);
$ext = "." . $splFile->getExtension();
// Don't take duplicates
if (! $allowDuplicate && isset($cache[$hash])) {
continue;
}
// Split File Name
list($category, $name, $subCategory) = explode("_", $splFile->getBasename($ext));
if (empty($category) || empty($name) || empty($subCategory)) {
$errors[] = "File ($splFile) does not have valid name format";
}
// New File path
$path = sprintf("%s/%s/%s", $dirDestination, $category, $subCategory);
if (! is_dir($path) and ! @mkdir($dirDestination, 0777, true)) {
throw new Exception("Destination Folder ($path) does not exist");
}
$fileTmp = $path . DIRECTORY_SEPARATOR . $splFile->getFileName();
if (is_file($fileTmp)) {
if (! $allowDuplicate) {
// Check if file is duplicate
$copyhash = md5_file($fileTmp);
if ($hash == $copyhash) {
continue;
}
}
$x = 1;
while(is_file($fileTmp)) {
$fileTmp = $path . DIRECTORY_SEPARATOR . $splFile->getBasename($ext) . "_" . $x . $ext;
$x ++;
}
}
if ($mode == FILE_COPY) {
if (! copy($splFile, $fileTmp))
$errors[] = "File ($splFile) Failed Copy";
} else {
if (! rename($splFile, $fileTmp))
$errors[] = "File ($splFile) Failed Move";
}
$cache[$hash] = true;
}
} catch (Exception $e) {
echo $e->getMessage();
}