-4

ファイルハンドルに関して質問があります。

ファイル: 「マーク、123456、HTCOM.pdf」

「ジョン、409721、JESOA.pdf

フォルダ:

「マーク、123456」

「マーク、345212」

「マーク、645352」

「ジョン、409721」

「ジョン、235212」

「ジョン、124554」

ファイルを正しいフォルダに移動するルーチンが必要です。上記の場合、ファイルとフォルダーの 1 番目と 2 番目の値を比較する必要があります。同じ場合は、ファイルを移動します。

投稿の補足:私はこのコードを持っていますが、正しく動作しますが、ファイルを移動するために名前とコードを確認するために変更する必要があります...関数の実装に混乱しています...

$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); 
    } 
}
4

1 に答える 1

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);
    }
}
于 2012-08-04T10:25:35.153 に答える