我有一个关于文件句柄的问题,我有:
文件: "马克,123456,HTCOM.pdf"
"约翰,409721,杰索亚.pdf
文件夹:
"马克,123456"
"马克,345212"
"马克,645352"
"约翰,409721"
"约翰,235212"
"约翰,124554"
我需要一个例程来将文件移动到正确的文件夹。在上面的情况下,我需要比较文件和文件夹中的第一个和第二个值。如果相同,我会移动文件。
补文:我有此代码,工作正常,但我需要修改以检查名称和代码以移动文件......我对实现功能感到困惑...
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
foreach (glob($pathToFiles . DIRECTORY_SEPARATOR . '*.pdf') as $oldname)
{
if (is_dir($dir = $pathToDirs . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_FILENAME)))
{
$newname = $dir . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_BASENAME);
rename($oldname, $newname);
}
}
作为一个粗略的草稿,并且仅适用于您的特定情况(或遵循相同命名模式的任何其他情况),这应该有效:
<?php
// define a more convenient variable for the separator
define('DS', DIRECTORY_SEPARATOR);
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
// get a list of all .pdf files we're looking for
$files = glob($pathToFiles . DS . '*.pdf');
foreach ($files as $origPath) {
// get the name of the file from the current path and remove any trailing slashes
$file = trim(substr($origPath, strrpos($origPath, DS)), DS);
// get the folder-name from the filename, following the pattern "(Name, Number), word.pdf"
$folder = substr($file, 0, strrpos($file, ','));
// if a folder exists matching this file, move this file to that folder!
if (is_dir($pathToDirs . DS . $folder)) {
$newPath = $pathToDirs . DS . $folder . DS . $file;
rename($origPath, $newPath);
}
}