I working on uploading images to the host machine.
How does my code look.
Also, I need help with being able to rename the file that gets uploaded and help with “Validating if FILE already Exists”
Please post replies respectfully.
[code] <?php
// Loop through file upload fields PICTURES from HTML FORM
foreach ($_FILES[“pictures”][“error”] as $key => $error) {
$size = $_FILES[“pictures”][“size”][$key];
$type = $_FILES[“pictures”][“type”][$key];
$name = $_FILES[“pictures”][“name”][$key];
// Validate if ERROR occured
if ($error == UPLOAD_ERR_OK) {
echo $name . “
”;
// Validate the FILE SIZE
if (($size < 100000) && ($size > 0)) {
echo “Size of $name is good: " . $size . “
”;
$size_okay = 1;
} else {
echo “The file size of " . $name . " is to large. 100KB is the Maximum”;
$size_okay = 0;
}
// Validate the FILE TYPE EXTENSION
$filename = explode(”.", $_FILES[“pictures”][“name”][$key]);
$filenameext = $filename[count($filename)-1];
if (($filenameext == “jpg”) || ($filenameext == “JPG”) || ($filenameext == “jpeg”)) {
echo $filenameext . “
”;
echo $name . " is a JPG file type.
“;
$type_okay = 1;
} else {
echo $name . " is NOT a JPEG or JPG file type… it is a “” . $filenameext . “”.
”;
$type_okay = 0;
}
// Validate the FILE NAME
if ($name == $name) {
echo "The " . $name . " is good.
";
$name_okay = 1;
} else {
echo "The " . $name . " is BAD.
";
$name_okay = 0;
}
// Validate if FILE already Exists
// ...... IF STATEMENT HERE... etc etc etc .......
// Uploading FILES
if (($size_okay == 1) && ($type_okay == 1) && ($name_okay == 1)) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$dirpath = "components/";
$img_name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, $dirpath . $img_name);
echo "<strong>" . $name . "</strong> has been sucessfully uploaded. <br />";
} else {
if ($size_okay == 0) {
echo "<strong>" . $name .
"</strong> has not been uploaded because the file size was greater then 100KBs.";
} elseif ($type_okay == 0) {
echo "<strong>" . $name .
"</strong> was not uploaded because the file type was not JPG.";
} elseif ($name_okay == 0) {
echo "<strong>" . $name .
"</strong> was not uploaded because the file already exists.";
} else {
echo "";
}
}
} else {
echo "An error has occured during the upload of <strong>" . $name . "</strong>
<br />1.) Make sure that the image file type is JPG or JPEG.
<br />2.) Make sure the the image file size is NOT larger then 100 KBs";
}
echo "<br /><br />";
}
?> [/code]