PHP使用特定名称移动文件

PHP使用特定名称移动文件,php,file,Php,File,我有10万多个文件名为RMA(编号)(日期)(时间)。jpg像RMA\u 12345\u 2015\u 10\u 12\u 17\u 00\u 35。jpg 如何像RMA_35200.*.jpg那样移动此文件?使用glob()查找这些文件并rename()移动它们 function moveFiles($source, $target) { // add missing "/" after target if(substr($target,-1) != '/') $target .= '

我有10万多个文件名为RMA(编号)(日期)(时间)。jpgRMA\u 12345\u 2015\u 10\u 12\u 17\u 00\u 35。jpg

如何像RMA_35200.*.jpg那样移动此文件?

使用
glob()
查找这些文件并
rename()
移动它们

function moveFiles($source, $target) {
  // add missing "/" after target
  if(substr($target,-1) != '/') $target .= '/';

  $files = glob($source);
  foreach($files as $file) {
    $info = pathinfo($file);
    $destination = $target . $info['filename'];
    rename($file, $destination);
  }
}

moveFiles('/where/my/files/are/RMA_35200_*.jpg', '/where/they/should/be/';
您可以使用以下命令:

$ mv RMA_35200_*.jpg new_path
或者使用php实现,例如:

<?php

$fromPath = __DIR__ . '/from';
$toPath = __DIR__ . '/to';

$files = glob("{$fromPath}/RMA_35200_*.jpg");

foreach ($files as $file) {
    $fileName = basename($file);
    rename($file, "{$toPath}/{$fileName}");
}

我必须同意其他评论,“使用glob()查找这些文件,并重命名()移动它们”,等等

但是,我要补充一点,文件名的preg_匹配。与文件名匹配的PERL正则表达式。我想这就是你在这些答案中可能遗漏的

foreach ($files as $file) {
    if (preg_match('/RMA_[0-9\-_]+.jpg/i', $file) {
        ...more code here...
    }
}

exec('mv/path/to/files/RMA_35200.*.jpg/destination/dir')另一个选项是scandir,不需要fnmatchregex,因为
glob()
支持通配符,如“RMA_35200.*.jpg”