Hi,
Currently i am uploading files to a tempFiles directory on my website. I would like to create a PHP script (or function basically) that will go through the tempFiles directory and move files with the ".zip" extension to directory A and move files with the ".pdf" extension to the directory B.
anyone know how i can do this?
thanks
craig
Comments
: Currently i am uploading files to a tempFiles directory on my website. I would like to create a PHP script (or function basically) that will go through the tempFiles directory and move files with the ".zip" extension to directory A and move files with the ".pdf" extension to the directory B.
: anyone know how i can do this?
:
: thanks
: craig
:
I figured it out on my own! Wow...what a great feeling. Below is what I did...may not be the fastest/best way..but it works. I will be adding some more features to it later.
just copy and paste the code into Notepad or something if u wanna use it...it doesnt look pretty below but it will look fine once pasted into a notepad or something.
[code]
<?php
echo '<html>';
echo '';
echo 'My Web Site Title';
echo '';
echo '';
/* DECLARE VARIABLES */
define ("STARTFROM", "2");
define ("ZERO", "0");
// DO CREATE DIR for files going to BIN and change permissions to 777 before the move
// then put them back to 775 after the move
$dirstr = $_GET['dir'];
$isDir = is_dir($dirstr);
if ($isDir == 1)
{
print("$dirstr is a valid directory [code: $isDir]
");
$files = scandir($dirstr);
moveIt($files);
}
else
{
print("$dirstr is not a directory [code: $isDir]
");
print("Go # files: $num");
$cur = ZERO;
$curArr = array();
$a = STARTFROM;
while ($a < $num)
{
print("File[$cur]: ");
print("$files1[$a]");
print("
");
$str = explode(".",$files1[$a]); // Break it in pieces.
$ext = end($str); // The last one has to be the extension.
switch($ext)
{
case 'txt':
rename("../tmpFiles/$files1[$a]", "../tut/$files1[$a]") or die ("Could not rename file");
$curArr[0] = $curArr[0] + 1;
break;
case 'zip':
rename("../tmpFiles/$files1[$a]", "../bin/$files1[$a]") or die ("Could not rename file");
$curArr[1] = $curArr[1] + 1;
break;
}
$cur++;
$a++;
}
print("
");
print("successfully moved $curArr[0] text files
");
print("successfully moved $curArr[1] zip files
");
}
echo '';
echo '';
?>
[/code]