如何根据PHP中的名称将多个.txt文件移动到文件夹中?

问题描述 投票:0回答:1

我有一个包含3个子文件夹和.txt文件的文件夹,如下所示:

enter image description here

我想要:

  • 如果文件以A500开头,则应将其移至子文件夹FR
  • 如果文件以A700开头,则将其移至ES子文件夹。
  • 如果文件以A900开头,则将其移动到PT子文件夹。

我已经完成了此功能,但是它不起作用...我的错误在哪里?

<?php

function deplacementFile( $path ) {

   $dir    = './test/';
   $allFiles = scandir($dir);

   foreach($allFiles as $file) {

        if (!in_array($file,array(".","..","FR", "ES", "PT")))
      { 

      $file = $dir.$file;
      $filename = basename( $file );

       //read the entire string
       $str = file_get_contents( $file );

       if ( strpos( $filename, 'A500_' ) === 0 ) {
        $dossierDestination1 = './test/FR/';
        if(!copy($dir, $dossierDestination1)) { 
            echo "error copy";
            } else { 
            if(!unlink($dir)) { 
                echo "error unlink"; 
            } 
        }
      }
        else if ( strpos( $filename, 'A700_' ) === 0 ) {
            $dossierDestination2 = './test/ES/';
            if(!copy($dir, $dossierDestination2)) { 
                echo "error copy";
                } else { 
                if(!unlink($dir)) { 
                    echo "error unlink"; 
                } 
            }
          }

       else if ( strpos( $filename, 'A900_' ) === 0 ) {
        $dossierDestination3 = './test/PT/';
        if(!copy($dir, $dossierDestination3)) { 
            echo "error copy";
            } else { 
            if(!unlink($dir)) { 
                echo "error unlink"; 
            } 
        }
      }
            else {

           return false;
       }
    }
  }
}

deplacementFile( './test/' ); 

echo "Successfully completed!";
?>
php copy strpos unlink
1个回答
0
投票

这是脚本的升级版本:

© www.soinside.com 2019 - 2024. All rights reserved.