我想知道你是否可以提供帮助。我有大量的文件看起来像这样:
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
}
}
?>
答案 0 :(得分:0)
那么你可以用一个空格来分割文件名,这将给你一个包含两个元素2014-02-10
和JB123456G
的数组,然后使用substring来剪切字符串的前两个元素,并基于此,您可以移动您的文件。
答案 1 :(得分:0)
explode将帮助您分割字符串:$array = explode(' ', $filename);
然后您可以将$array[1]
的两个第一个元素(例如JB123456G)子串,例如此时$firstTwoLetters = string substr ($array[1], 0, 2);
使用PHP在线手册,它将帮助您了解所有PHP功能的所有内容。