0

Wonder if you can help, i'm new to php and am stuck. I have loads of files that look like this:

2014-04-01 NS122345 - The date, the initials of the person and there employee code.

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.

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:

Attempt - Array

$dir    = JPATH_BASE . DS . "upload";
$basename = basename($dir);
$array = scandir($dir);
$filename = basename($dir[0]);
print_r($array);
print_r($basename);

Any help is much appreciated. Or if you can point me to examples/documentation that'll be great.

Thanks

Simpson
  • 42
  • 7

1 Answers1

2

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);
    }
}
MIvanIsten
  • 481
  • 3
  • 7
  • The page seems to go blank when i add the above in. So ran it on php code checker and it flags up at: PHP Syntax Check: Parse error: syntax error, unexpected '{' in your code on line 72 if(!is_file($dir.DS.$file){continue;} – Simpson Oct 27 '14 at 14:29
  • OH WOW!!!!!! Thank you so much it worked. I have all but one error left: Undefined offset: 1 line 74 which is: $firstTwoLetters = substr($array[1], 0, 2);. – Simpson Oct 27 '14 at 14:45
  • Maybe there is a file without a space in the name. Add `if(count($array)<2){ continue; }` before it. – MIvanIsten Oct 27 '14 at 14:47
  • AH Yes Thank you so much. If anyone can give you a vote on this answer they should i wish i could i need 15 rep to add vote – Simpson Oct 27 '14 at 14:48
  • Thank you so much, just on a side note say i wanted to automatically create a directory if i doesn't exist. For example it reads NS but there isn't a "Nathan Saunders" Folder how could i go about automatically creating that directory? Last thing i just want to add to it – Simpson Oct 27 '14 at 15:33
  • `if(is_dir($folders[$firstTwoLetters])||mkdir($folders[$firstTwoLetters],0777,1)){ rename(...); }` – MIvanIsten Oct 28 '14 at 14:14
  • I've been asked to change one small part, if you have any ideas. I've been told there are going to be over 2000 employees so i would need to add to the folder array 2000 folders which us going to take ages. Is there a way to create and move automatically by reading the $firstTwoLetters? – Simpson Oct 31 '14 at 13:01
  • replace `$folders[$firstTwoLetters]` with `foldername($firstTwoLetters)` and write a `function foldername($firstTwoLetters){ ... }` returning the folder name calculated from the two letters – MIvanIsten Oct 31 '14 at 13:32
  • Ah ok whats the { ... }? – Simpson Oct 31 '14 at 15:27
  • and the same with { rename(...); }? What does the ... mean? – Simpson Oct 31 '14 at 15:28
  • { ... } is placeholder, where you have to write your code – MIvanIsten Nov 03 '14 at 12:10
  • Oh i see so i need to wrap the function in and around the if statement like so: function foldername($firstTwoLetters){ if(is_dir($foldername[$firstTwoLetters])||mkdir($foldername[$firstTwoLetters],0777,1)) rename($dir.DS.$file,$folders[$firstTwoLetters].DS.$file); } Seems to stop at the function/if statement any ideas? – Simpson Nov 03 '14 at 13:01
  • ok got slightly further by editing it to instead of a function to this: $foldername = $firstTwoLetters; if(is_dir($foldername[$firstTwoLetters])||mkdir($foldername[$firstTwoLetters],0777, true)) rename($dir.DS.$file,$foldername[$firstTwoLetters].DS.$file); It reads only the first letter and creates the folder but creates it where the code is in the components not in the upload. Is there a way to set it to read and create a folder not just on the first letter but also on 2nd letter and the folder is in the upload folder, which i defined as $dir = JPATH_BASE . DS . "upload"; – Simpson Nov 03 '14 at 15:43