首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -如果文件名包含......然后移动到目录

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

Stack Overflow用户
提问于 2014-10-25 00:06:27
回答 2查看 557关注 0票数 0

我想知道你是否能帮上忙。我有大量的文件,如下所示:

2014-02-10 JB123456G

还有这个:

2012-02-01 NA657432B

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

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
    <?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


            }
}


?>
EN

回答 2

Stack Overflow用户

发布于 2014-10-25 00:20:08

你可以用一个空格来拆分文件名,这将给你一个包含两个元素2014-02-10和JB123456G的数组,然后使用substring来剪切字符串的前两个元素,在此基础上你可以移动你的文件。

票数 0
EN

Stack Overflow用户

发布于 2014-10-25 01:10:09

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26551832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档