2012-07-05 157 views
-4

我有一个关于文件句柄的一个问题,我有:移动文件到特定文件夹

文件: “马克,123456,HTCOM.pdf”

“约翰,409721,JESOA.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); 
    } 
} 
+1

是的,对不起,我已经发布代码,也... – user1504222 2012-07-05 14:17:34

回答

0

作为一个粗略的草稿和东西,将只与您的特定情况下(或相同的命名模式下任何其他情况下)工作,这应该工作:

<?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); 
    } 
} 
相关问题