Uploading images to different directories

I have a simple PHP script that lets the user upload images to a directory on my server.

$uploaddir = ‘images/’;
$uploadfile = $uploaddir . basename($_FILES[‘userfile’][‘name’]);

if (move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $uploadfile)) {
echo “File is valid, and was successfully uploaded.”;
}

Which works fine. This scipt as you can see will upload an image to a directory, path: images/imagename.jpg. What I’m trying to do is set a variable for “images/” called “$folder”. So depending what link the user clicked on to get to the upload page, this variable changes allowing images to be uploaded to different directories. So my first thought was to simple change this line to get it working:
$uploaddir = ‘images/’;
to
$uploaddir = $folder;
The variable $folder is being read properly but when I submit the form to upload the file it leaves out the $folder and just writes the image name uploading it to the same directory the file is in.
I have tried 100 different ways to get this to work. I have found a way to get it to work but its not very sufficient.
Can someone please help.

Thank you
John

sure… from the other page its coming from a form as:

<form action=‘addimage.php’ method=‘post’>
<input type=‘hidden’ name=‘id’ value=’$id’ />
<input type=‘hidden’ name=‘folder’ value=’$folder’ />
<input type=‘submit’ value=‘add pictures’ />
</form>

which gets the values from a database. So it passes the clients ID and the name of their folder I want them to be able to upload to.

On the other page I have.

$dbcnx = @mysql_connect(‘myserver’, ‘name’, ‘password’);
if (!$dbcnx) {

$id = $_POST[‘id’];
$employee = @mysql_query(

“SELECT name, folder, FROM clients WHERE id=’$id’”);

if (!$employee) {
exit(’<p>Error fetching employee details: ’ .
mysql_error() . ‘</p>’);
}

$employee = mysql_fetch_array($employee);

$name = $employee[‘name’];
$folder = $employee[‘folder’];

Then I have the form…

<form enctype=‘multipart/form-data’ action=’’ method=‘post’>
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type=‘hidden’ name=‘MAX_FILE_SIZE’ value=‘30000’ />
<input name=’‘userfile’’ type=‘file’ />
<input type=‘submit’ value=‘Upload File’ />
</form>

What I have done to get it working somewhat:

<form enctype=‘multipart/form-data’ action=’’ method=‘post’>
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type=‘hidden’ name=‘MAX_FILE_SIZE’ value=‘30000’ />
<input name=’$folder’ type=‘file’ />
<input type=‘submit’ value=‘Upload File’ />
</form>

$folder1 = ‘john/’;
$folder2 = ‘mark/’;

//$uploaddir = $foldername;
$uploadfile = $folder1 . ($_FILES [‘john’][‘name’]);
$uploadfile2 = $folder2 . ($_FILES [‘mark’][‘name’]);

if (move_uploaded_file($_FILES[‘john’][‘tmp_name’], $uploadfile)) {
echo “File is valid, and was successfully uploaded.”;
}
if (move_uploaded_file($_FILES[mark][‘tmp_name’], $uploadfile2)) {
echo “File is valid, and was successfully uploaded.”;
}

so the $folder value gets passed to the form as (John or mark) and then when the form is submited the proper function gets trigered. This works but as you can see its not sufficient, everytime a new client joins I have to add another function for them. This is my first dynamic website I am building, I’m new to PHP and Mysql, I’m loving it but I’m struggling at times.

Thanks in advance
John