I've been working to have it so users of a database can upload a few images. The images would be stored in an image directory and have the file name of username-pic1, username-pic2...
The code I have below pulls the user name from a previous posted form.
There are several echo statements that are there only so I can see that variables are being passed along.
Right now all the variables are being held fine until the form is processed and then the username variable goes dead. So the echo shows that it will be uploading the file as "username-pic1" but instead it uploads it as "-pic1".
I've been looking at this way too much and I've have tried a number of minor variations but with no luck.
Here is the code.
<form enctype="multipart/form-data" action="" method="post">
First picture: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<?PHP
session_start();
?>
<?php
$user = $_POST['record'];
echo "username = $user<br><BR>";
$success = 0;
$fail = 0;
for ($i=0;$i<1;$i++)
{
$loc1 = "images/" ;
$loc2 = "pic1." ;
$uploaddir = $loc1 . $user . "-" . $loc2 ;
echo "new file name = ".$uploaddir.$loc2 ;
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploaddir . $ext))
{
$success++;
echo "File $success uploaded successfully.";
echo "uploadfile = ";
echo $uploadfile;
echo "<br /> user = ";
echo $_POST['record'];
echo "<br /> uploaddir = ";
echo $uploaddir;
echo "<br /> new file name = ";
echo $uploaddir.$ext;
}
else
{
echo "Error Uploading the file. Please try again.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
?>
Does anyone have any suggestions as to what I am missing here?
Thanks,
Jim