【发布时间】:2019-07-14 22:30:54
【问题描述】:
在网页中,我有 2 个选项卡和 1 个提交按钮。我首先使用 type=text 制表符输入。在 type=file 的第二个输入中。我想用 1 个提交发送两个标签。但问题不在这里。在第二个选项卡中,我需要在发送文件之前检查文件是否有错误。因此,如果出现错误,则提交按钮无效。我怎样才能做到这一点? 现在我只在发送它们后检查它们是否有错误。当某些文件出现错误时,上传失败并返回错误页面。并且所有其他没有错误的文件从输入中消失。 第一个标签 http://prntscr.com/mnzc79 。第二个标签http://prntscr.com/mnzca3。 这里所有的输入都是大学学生的文件。所以我可以只使用多上传。因为它们都是不同的文件。我无法理解通过错误检查上传许多文件输入的机制。 这是我的代码
Check if text inputs uploaded. If files were set then I push them to the array to use in query.
if(!empty($_POST["first_name"])){
$keys=$keys."first_name,";
$first_name=$_POST["first_name"];
$values=$values."'".$_POST['first_name']."',";
}
if(!empty($_POST["last_name"])){
$keys=$keys."last_name,";
$last_name=$_POST["last_name"];
$values=$values."'".$_POST['last_name']."',";
}
...
...
Check if files uploaded
if(isset($_FILES["zayavlenie_o_zachisleniy"]["name"]) && !empty($_FILES["zayavlenie_o_zachisleniy"]["name"])){
array_push($uploads,"zayavlenie_o_zachisleniy");
upload("zayavlenie_o_zachisleniy");
}
...
...
Here checks file for error and upload
function upload($filename){
if (!file_exists('student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo)) {
mkdir('student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo, 0777, true);
}
$target_dir = 'student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo.'/';
$target_file = $target_dir . basename($_FILES[$filename]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
.....
}
// Check if file already exists
if (file_exists($target_file)) {
.....
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
.....
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
.....
} else {
if (move_uploaded_file($_FILES[$filename]["tmp_name"], $target_file)) {
array_push($values,$target_file);
echo "The file ". basename( $_FILES[$filename]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
$sql="UPDATE students set $set where id='$student_id'";
for($i=0;$i<sizeof($uploads);$i++){
if($i==(sizeof($uploads)-1)){
$sql=$sql."'".$uploads[$i]."'";
}
else{
$sql=$sql."'".$uploads[$i]."',";
}
}
$db->insert($sql.")");
【问题讨论】:
标签: php