My original posting here, in the PHP section said this:
I’m looking for help adding to this Upload Form. Currently if you upload the wrong size or wrong format the corresponding error message appears, but the erroneous action can still proceed.
How can I halt the ‘Submit’ until the correct file format is chosen?
How can I halt the ‘Submit’ until the correct size is chosen?
Here’s some of the Form’s html:
<input class="button-form3" type="submit" value="Submit >>" name="B3" />
<input class="upload-form-input" type="hidden" name="form_submitted" value="yes" />
Here’s the Form’s php code:
if ($form_submitted == 'yes')
{
$allowedExts = array("doc", "docx", "gif", "jpeg", "jpg", "txt", "rtf", "pdf", "png", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );
if(!in_array($extension,$allowedExts) && $_FILES["file"]["name"] != "" )
{
echo ("<div id=\"errorMessage\">Error: Invalid File Name </div>");
}
else if ($_FILES["file"]["size"] >= 100000)
{
echo ("<div class=\"errorMessage1\">Error: Image File Size Exceeds Limit </div>");
}
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
$uploadedFile = $_FILES["file"]["tmp_name"];
if($uploadedFile != NULL)
{
move_uploaded_file($uploadedFile, "upload/" . $thumbnail);
}
else
{
copy("upload/NoInfoAvailable.png", "upload/" . $thumbnail);
}
$_SESSION['thumbnail'] = $thumbnail;
$file_location = '<a href="http://www....com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';
}
I was referred to the Javascript section, and it was suggested:
“you are just printing “Error: Invalid File Name”. At this point, it should still be in a temp directory (normally, it remains there JUST long enough for it to be moved elsewhere.) Instead of moving the file to another folder, just delete it (if the temp folder doesn’t already do that automatically.)”
So, can PHP code be added to delete an incorrect file format, or incorrect file size, when the appropriate error appears? (so that I can change the error messages to essentially say “upload blocked try again”). If so, can you provide a code example, please?
EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.