I have been searching all over and found examples, but either the code is not working, I have typed it wrong, or there is something wrong with my server.
I am trying to allow users to load images (jpg only) from a page where they must be logged in. The form fields are named photo1, photo2, and photo3. I want to check the file size as well to keep under 500kb. It seems the photos are never recognized by the script. Here is the code.
<?
$path = "/www/images/members/";
//do a for loop for all three photos
for ($a = 1; $a <= "3" ; $a++) {
if (empty($photo$a)) {
$error = "You did not select file $a to upload";
print $error;
} else {
//a file was uploaded, store the name, save it as $username$a
$maxfilesize=500000;
if ($FILES['photo$a']['size'] > $maxfilesize) {
$error = "The selected file is too large, it must be less than
$maxfilesize bytes.";
unlink($FILES['photo$a']['tmp_name']);
print $error;
} else {
//the file is under the specified number of bytes, check type
if($FILES['photo$a']['type'] == "image/pjpeg" || $FILES['photo$a']['type'] =="image/jpeg") {
//save in standard format
$newfile = "$path$username$a.jpg";
//the file is the correct format do something
copy($photo$a, $newfile);
//add the filename to the photo$a record in the db
$sql = "UPDATE members SET photo$a = '$username$a.jpg' WHERE name = '$username'";
$result = @($sql,$connection) or die(mysql_error());
} else {
$error = "This file type is not allowed";
unlink($_FILES['photo$a']['tmp_name']);
print $error;
}
}
}
}
?>