PHP - 爆炸/移动/文件名 [英] PHP - Exploding / Moving / Filename
问题描述
不知道你能否帮帮忙,我是新来的PHP和我坚持。我有文件加载看起来像这样的:
Wonder if you can help, i'm new to php and am stuck. I have loads of files that look like this:
2014年4月1日NS122345 - 日期,该人的声母和员工有code。
2014-04-01 NS122345 - The date, the initials of the person and there employee code.
我希望能够有NS或JB或Ga的文件移动到有相关文件夹/目录。因此,对于NS它将进入弥敦道桑德斯文件夹中,JB到乔·贝利文件夹。
I want to be able to move the files that have NS or JB Or GA into there relevant folder/directories. So for NS it would go into the Nathan Saunders Folder, for JB into the Joe Bailey folder.
一个少数人对我说使用爆炸和SUBSTR了拆分文件名,然后我想通过数组搜索。有人说用这样的:
A few have said to me to use the explode and substr to split the filename up and then i guess search through the array. Someone said to use this:
$array = explode(' ', $filename);
$firstTwoLetters = string substr ($array[1], 0, 2);
但我得到的变量名的错误没有定义。
But i get the error of variable filename not defined.
不幸的是我不知道。我通过论坛搜索论坛和发布的帖子后,我想出了这个迄今为止后:
Unfortunately i have no clue. I have search through forums after forums and postings after postings, i have come up with this so far:
if(JFile::exists($searchpath .DS. '.doc')){
JFile::move($searchpath .DS. '.doc', JPATH_BASE .DS. 'upload' .DS. 'Nathan' .DS. '.doc'); }
我的目录结构是这样的:
My directory structure looks like this:
root/wan/upload - Where files/images/docs are stored. Inside upload folder i have:
>2014-04-08 NS6565.doc
>2012-01-03 JB8932.doc
>2013-02-01 GA5434.doc
>etc
root/wan/administrator/components/com_upload - where my code is stored
问题是我有几百万的这些文件,并在上面我给的例子的开始日期将每一个变化。
Problem is i have millions of these files, and the date at the start of the example i gave above will change for each.
我试图把它们放入一个数组但是从那里不知道如何再拆/调用合适的人摆在正确的文件夹:
I have tried putting them into an array but not sure from there how to then split/call the right one to put in the right folder:
尝试 - 阵列
$dir = JPATH_BASE . DS . "upload";
$basename = basename($dir);
$array = scandir($dir);
$filename = basename($dir[0]);
print_r($array);
print_r($basename);
任何帮助很多AP preciated。或者,如果你可以点我的例子/文档将是伟大的。
Any help is much appreciated. Or if you can point me to examples/documentation that'll be great.
感谢
推荐答案
假设DS是DIRECTORY_SEPARATOR
Assuming DS is DIRECTORY_SEPARATOR
$dir = JPATH_BASE . DS . "upload";
$folders = array('NS'=>'/path/to/Nathan/Saunders/Folder','JB'=>'/path/to/Joe/Bailey/folder');
$files = scandir($dir);
foreach($files AS $file){
if(!is_file($dir.DS.$file)){ continue; }
$array = explode(' ', $file);
if(count($array)<2){ continue; }
$firstTwoLetters = substr($array[1], 0, 2);
if(!empty($folders[$firstTwoLetters])){
rename($dir.DS.$file,$folders[$firstTwoLetters].DS.$file);
}
}
这篇关于PHP - 爆炸/移动/文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!