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,
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);
Related
with this code I get filename from the current folder
<?php
$mydir = dirname(__FILE__);
$myfiles = array_diff(scandir($mydir), array('.', '..'));
$arrsingleresult = str_replace('.php', '', $myfiles);
print_r($arrsingleresult);
?>
Now how can I get filenames from one folder back ? (../ like this) any work around ?
Please use double time dirname() function to back one level
dirname(dirname(__FILE__));
I want to rename all files in a folder with random numbers or characters.
This my code:
$dir = opendir('2009111');
$i = 1;
// loop through all the files in the directory
while ( false !== ( $file = readdir($dir) ) ) {
// do the rename based on the current iteration
$newName = rand() . (pathinfo($file, PATHINFO_EXTENSION));
rename($file, $newName);
// increase for the next loop
$i++;
}
// close the directory handle
closedir($dir);
but I get this error:
Warning: rename(4 (2).jpg,8243.jpg): The system cannot find the file specified
You're looping through files in the directory 2009111/, but then you refer to them without the directory prefix in rename().
Something like this should work better (though see the warning about data loss below):
$oldName = '2009111/' . $file;
$newName = '2009111/' . rand() . (pathinfo($file, PATHINFO_EXTENSION));
rename($oldName, $newName);
Of course, you may want to put the directory name in a variable or make other similar tweaks. I'm still not clear on why you're trying to do this, and depending on your goals there may be better ways of reaching them.
Warning! The approach you are using could cause data loss! A $newName could be generated that is the same name as an existing file, and rename() overwrites target files.
You should probably make sure $newName doesn't exist before you rename().
I'm using this code to get a listing of every folders/subfolders of a repertory :
$path = realpath($userdir);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
echo "$filename\n";
}
However it displays something like this, wich add dot or double dots and displays the folder twice :
C:\wamp\www\gg\ftp\repository\user\mister.
C:\wamp\www\gg\ftp\repository\user\mister..
C:\wamp\www\gg\ftp\repository\user\mister\apatik.
C:\wamp\www\gg\ftp\repository\user\mister\apatik..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx..
Instead of something cleaner like this :
C:\wamp\www\gg\ftp\repository\user\mister
C:\wamp\www\gg\ftp\repository\user\mister\apatik
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx
Is there a way ? I was previously using the function glob
glob($sub . '/*' , GLOB_ONLYDIR);
which was displaying the folder correctly but i couldn't get it to be recursive to display subfolders aswell.
Thanks
you need something like this
set_time_limit(0);
function scanDirectory($sub = ''){
$folders = glob($sub . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
echo "$folder<br />";
scanDirectory($folder);
}
}
scanDirectory();
and this will list all folders on current drive
EDIT
$folders = glob($sub . '/*' , GLOB_ONLYDIR); will get all the folders in specified directory.
foreach($folders as $folder) will loop over folders array.
$sub will be the folder name that will be explored.
so, these are the folders
A B C
$folders will have like $folders['A', 'B', 'C']
and in loop, each A B and C will be passed as $sub to check either it has more folders in it or not.
I have several files in a folder and i want to count them.
$folder = "images";
$allPics = scandir($folder);
$result = sizeof($allPics);
echo $result;
The result is 350 but it should be 348. I don't get it why it is showing me the result +2?
Am i missing something?!
http://php.net/manual/en/function.scandir.php
When looking at the documentation you can see the function return both '.' and '..', that's why you're having 2 more than you should have.
You can use this:
array_diff(scandir($folder), array('..', '.'));
To get rid of the dots you don't wanna have.
You are using the unix system and it have, 2 pointers in each directory, the pointer for the parent dirrectory that usualy is notted with .. and the pointer to the current directory that is notted as .
inside ajax folder, there are files:
json.php, load.php, script.php
main.php
<?php
$dir = dir("ajax");
while (($file = $dir->read()) !== false)
{
echo "filename: " . $file . "<br />";
}
$dir->close();
It show:
filename: .
filename: ..
filename: json.php
filename: load.php
filename: script.php
Question:
In the result, what does the first two items mean? . .. ?
Those are the current (.) and parent (..) directories.
You can get rid of them in following way:
$result = array_diff($result, array('..', '.'));
. and .. are "directories" which equate to the current directory, and parent directory, respectively.
for example, if you are in the ajax folder, json.php can also be accessed with ./json.php.
on the other hand, ../json.php will look for the file in the parent folder (same directory as the ajax folder).
. is current folder.
.. is parent folder. Any folders on window and linux have two this.
If you want to read some type of file, you can use glob function of php.
$files = glob($dirpath.'/*.php');
Single dot: . This represents current directory.
Double dot: .. This represents parent directory.