I have two problems with this code.
1. when i refresh the page it uploads the file again.
2. how to i validate the form if required fields are not filled ?
i have added the code validation but its not working and there are no errors.
<?php
if (isset($_POST['upload']))
if ($_FILES['userfile']['size'] > 0) {
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$allowed_filetypes = array(
'.jpg',
'.jpeg',
'.png',
'.gif'
);
$requiredFields = array(
'imagetitle',
'pickdate',
'option',
'list1',
'list2',
);
$errors = array();
foreach($_POST AS $key => $value)
{
// is this a required field?
if(in_array($key, $requiredFields) && $value == '')
$errors[] = "The field $key is required.";
}
$max_filesize = 1445760;
$description = $_POST['imagetitle'];
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1);
if (!in_array($ext, $allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if (filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$date= $_POST['pickdate'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if (!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
} //!get_magic_quotes_gpc()
include 'config.php';
include 'opendb.php';
$query = "INSERT INTO upload (column1,column2,column3,column4,column5,column6,) " . "VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$date','$description')";
mysql_query($query) or die('Error, query failed');
include 'closedb.php';
echo "<br>File $fileName uploaded<br>";
} //isset($_POST['upload']) && $_FILES['userfile']['size'] > 0
else {
echo 'Please Upload an Image.';
} //isset($_POST['upload'])
?>
<form action="uploadform.php" method="post" enctype="multipart/form-data">
<br><br>
Image Title: <input type="text" name="imagetitle" ><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="userfile" id="userfile" ><br><br>
Choose Date: <input type="date" name="pickdate" > <br><br>
<input id="button_1" type="radio" name="option" value="button1" /><label for="button_1" >Button 1</label>
<input id="button_2" type="radio" name="option" value="button2" /><label for="button_2" >Button 2</label><br><br>
<select name="list1">
<option value="">Select Options</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<br><br>
<select name="list2">
<option value="">Select Type</option>
<option value="type1">Option 1</option>
<option value="type2">Option 2</option>
<option value="type3">Option 3</option>
</select>
<br><br>
<input name="upload" type="submit" class="box" id="upload" value=" Upload " >
</form>