0

See previous Question which was partly answered but there has been a change in requirements for the script: PHP - Exploding / Moving / Filename

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.

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

This is my php code for moving, creating and checking the filename and putting it in the correct folder:

$dir    = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$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, 9);
    $foldername = $firstTwoLetters;
        if(is_dir($folders[$firstTwoLetters])||mkdir($foldername[$firstTwoLetters],0777, 1)) 
            rename($dir.DS.$file,$foldername[$firstTwoLetters].DS.$file);

That code currently reads the filename if its already in the array "folders" it moves to the correct folder, I have changed it recently to make the folder automatically reading whatever file is in the upload section, but the problem comes when making the folder, the mkdir seems to make the directory:

1) in the wrong place it makes it where the code is stored which is in the com_upload section instead of making it in the upload folder. 2) Names it wrong it takes the first letter not the letters or numbers after it. E.g. "2014-04-08 NS6565.doc", makes the directory "N"

Any help to fixing those 2 problems would be great.

Thanks,

Community
  • 1
  • 1
Simpson
  • 42
  • 7
  • `$firstTwoLetters = substr($array[1], 0, 9);` is never gonna return 2 characters. `substr` takes (string, starting point, lenght). Take a look at the [manual](http://nl1.php.net/manual/en/function.substr.php) – Michel Nov 05 '14 at 12:01
  • Yh it will return 9 which is what i want as there was a change to all the letters and numbers after the space. But it creates the folder but names it using just one letter – Simpson Nov 05 '14 at 12:04
  • You don't need to use `/var/www/vhosts.....`. You can use `JPATH_BASE` which will return the root of your Joomla installation. You can then define the rest of the path from there on, like you have for the `$dir` variable – Lodder Nov 05 '14 at 12:12

1 Answers1

1

1) If you want to create the directory in another place or you use a relative path from the directory your code is or you use an absolute path. 2) When you are creating the directory you use $foldername but it isn't a name of any directory. It's instead the name of the file. Also, you use it as an array when it's a string (so it only take one char)

Try this:

$dir    = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$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, 9);
    $foldername = substr($firstTwoLetters,0,2);
        if(is_dir($dir. DS . $foldername)||mkdir($dir. DS . $foldername,0777, 1)) 
            rename($dir.DS.$file,$dir . DS . $foldername . DS.$file);
Serpes
  • 672
  • 4
  • 14
  • Quick edit to that it doesn't move if the directory already exists, is there a way to say that when it reads it and it exists and still moves to the correct directory – Simpson Nov 05 '14 at 12:14
  • Just change the if condition:is_dir($dir. DS . $foldername)||mkdir($dir. DS . $foldername,0777, 1) – Serpes Nov 05 '14 at 12:16