此代码没有错误,但是当我上传带有部分/全部空字段的文件时,文件会上传。但是当它不检查其他字段是否为空时。我的代码中缺少什么? 如何检查所有必填字段是否已填写?
form.php的
<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" onclick="return validateForm()" id="upload" value=" Upload " >
</form>
formhander.php
<?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'])
header('Location: form.php');
exit;
?>
答案 0 :(得分:2)
像这样检查
if (isset($_POST['upload']) && is_uploaded_file($_FILES['userfile']['tmp_name']) )
答案 1 :(得分:-1)
使用HTML5,您现在可以在没有PHP的情况下检查字段是否已填充。只需在required=""
标记内添加<input ... />
,如果未填写,则会弹出一条消息,告知用户该字段是必填字段。
修改强>
这个解决方案只能帮助客户,就像Mike在评论中所说的那样。如果您仅将此用于“验证”,则非常不安全。