PHP-如果文件名包含...然后移动到目录


PHP - If Filename Contains...... then move to directory

我想知道你是否能帮忙。我有很多文件看起来像这样:

2014-02-10 JB123456G

这个:

2012-02-01 NA657432B

在php中,我想将这些文件过滤到相关目录中。例如"2014-02-10 JB123456G"进入"JB"文件夹。我想用If语句来查找文件名是否包含字母"JB",然后移到JB目录中。目前我有:

if(JFile::exists($searchpath .DS. 'JB.png')){
JFile::move($searchpath .DS. 'JB.png', JPATH_BASE .DS. 'upload' .DS. 'JB' .DS. 'JB.png'); }

我知道我需要把"JB.png"改成一些东西,但不太确定是什么,有人推荐Regex做了一些研究,但不确定如何添加。它确实会把JB.png文件移到JB文件夹中,但显然它只会移动确切的文件名,不会移动文件名:2014-02-10 JB123456G.png.

感谢您的帮助。

感谢

更新:

这是我的完整php代码:

    <?php
echo "Start Moving Files to Folder - Steps 1-7 <br><br>"; 
define( '_JEXEC', 1); 
define('JPATH', dirname(__FILE__) );
if (!defined('DS')){
    define( 'DS', DIRECTORY_SEPARATOR );

$parts = explode( DS, JPATH );  
$script_root =  implode( DS, $parts ) ;
// check path
$x = array_search ( 'administrator', $parts  );
if (!$x) exit;
$path = '';
for ($i=0; $i < $x; $i++){
    $path = $path.$parts[$i].DS; 
}
// remove last DS
$path = substr($path, 0, -1);
if (!defined('JPATH_BASE')){
    define('JPATH_BASE', $path );
}    
if (!defined('JPATH_SITE')){
    define('JPATH_SITE', $path );
}    
/* Required Files */
require_once ( JPATH_SITE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_SITE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
echo "Step 1 - Required Files Set<br><br>";
//Import filesystem libraries. 
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.user.user');
echo "Step 2 - Import Filesystem libraries <br><br>";
//First we set up parameters
$searchpath = JPATH_BASE . DS . "upload";
echo "Step 3 - Set Search Parameters to directory root/upload<br><br>";
//Then we create the subfolder called Images
//if ( !JFolder::create($searchpath . DS ."Images") ) {
   //Throw error message and stop script
//}
//echo "Step 4 - Create Subfolder if necessary <br><br>";
echo "Step 5 - Read all png files and place them into an array <br><br>";
//Now we read all png files and put them in an array.
$png_files = JFolder::files($searchpath,'.png');
$doc_files = JFolder::files($searchpath,'.doc');

echo "Step 6 - Move all files into new folder <br><br>";
//Now we need some stuff from the JFile:: class to move all files into the new folder
if(JFile::exists($searchpath .DS. 'j.png')){
    JFile::move($searchpath .DS. 'j.png', JPATH_BASE .DS. 'upload' .DS. 'Nathan' .DS. 'j.png'); }

echo "Step 7 - Move the whole subdirectory to the root of the component<br><br>";
//Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . DS. ".png",JPATH_BASE) ) {
   //Redirect with perhaps a happy message
} else {
   //Throw an error

            }
}

?>

您可以用空格拆分文件名,这将为您提供一个由两个元素2014-02-10JB123456G组成的数组,然后使用子字符串剪切字符串的前两个元素,并在此基础上移动文件。

分解将帮助您拆分字符串:$array = explode(' ', $filename);然后您可以将$array[1]的两个第一个元素(即JB123456G)进行子串处理,例如,此时的$firstTwoLetters = string substr ($array[1], 0, 2);

使用PHP在线手册,它将帮助您了解有关所有PHP函数的一切。

http://php.net/manual/fr/function.substr.php