上传文件的PHP表单验证
在php中验证文件上载时遇到问题。基本上,代码没有看到文件是空的,我整个下午都在看,看不出来。我相信这很简单。如果有人能解决我的问题,我将不胜感激。提前谢谢 这是一个未完成的项目,我只是停留在验证阶段上传文件的PHP表单验证,php,forms,validation,Php,Forms,Validation,在php中验证文件上载时遇到问题。基本上,代码没有看到文件是空的,我整个下午都在看,看不出来。我相信这很简单。如果有人能解决我的问题,我将不胜感激。提前谢谢 这是一个未完成的项目,我只是停留在验证阶段 <?php // Link to separate file that holds the connection info include 'connection.php'; $error = ""; if ($_POST){ // Ba
<?php
// Link to separate file that holds the connection info
include 'connection.php';
$error = "";
if ($_POST){
// Back up to JS Validation
if(!$_POST['username']){
$error = "No username<br>";
}
if(!$_POST['password']){
$error .= "No password<br>";
}
if(!$_POST['email']){
$error .= "No email<br>";
}
// Test to see if a file has been posted
if(!empty($_FILES['uploaded_file'])){
// Set the upload path
$path = "uploads/";
// Append the filename
$path = $path . basename( $_FILES['uploaded_file']['name']);
// Need to move the file to folder
// If successful display success message or error
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
// File has been uploaded create user
$query = "INSERT INTO ";
} else {
// File not uploaded, account not created
echo "There was an error uploading the file, You have not create an account";
}
} else {
$error .= "No file uploaded";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<style>
h2 {
margin-top: 50px;
}
.signupContainer {
background-color: darkgray;
width: 400px;
border-radius: 10px;
margin: auto;
margin-top: 20px;
}
.alertContainer {
width: 400px;
margin: auto;
margin-top: 20px;
}
.signupFormContainer {
padding: 20px 50px 20px 50px;
margin: auto;
}
</style>
<title>NotePad App</title>
</head>
<body>
<h2 class="text-center">NotePad App</h2>
<?php
// Back up to JS validation - display errors
if ($error){
?>
<div class="alertContainer">
<div class="alert alert-danger">
<strong>Error!</strong> There were errors on your form:<br><?php echo $error ?>
</div>
</div>
<?php
}
?>
<div class="signupContainer">
<div class="signupFormContainer">
<form enctype="multipart/form-data" action="index.php" method="POST" id="createUser">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Create a Username" name="username">
<div class="invalid-feedback">
Please enter a username
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Enter a Password" name="password">
<div class="invalid-feedback">
Please enter a password
</div>
</div>
<div class="form-group">
<label for="password">Email Address</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email address" name="email">
<div class="invalid-feedback">
Please enter a valid email address
</div>
</div>
<div class="form-group">
<label for="password">Upload Text File</label>
<input class="form-control" type="file" id="uploaded_file" name="uploaded_file" accept="text/plain">
<div class="invalid-feedback">
Please select a txt file
</div>
</div>
<div class="form-group text-center">
<button type="button" class="btn btn-success" id="signup">Sign Up!</button>
</div>
</form>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<script type="application/javascript">
function clearClass(){
$("#username").removeClass("is-invalid");
$("#password").removeClass("is-invalid");
$("#email").removeClass("is-invalid");
$("#uploaded_file").removeClass("is-invalid");
}
$("#signup").click(function(){
clearClass();
var frmError = false;
// if ($("#username").val() == ""){
// $("#username").addClass("is-invalid");
// frmError = true;
// }
// if ($("#password").val() == ""){
// $("#password").addClass("is-invalid");
// frmError = true;
// }
// if ($("#email").val() == ""){
// $("#email").addClass("is-invalid");
// frmError = true;
// }
// if ($("#uploaded_file").val() == ""){
// $("#uploaded_file").addClass("is-invalid");
// frmError = true;
// }
if (!frmError){
$('#createUser').submit();
}
});
</script>
</body>
</html>
氢{
边缘顶部:50px;
}
.注册容器{
背景色:暗灰色;
宽度:400px;
边界半径:10px;
保证金:自动;
边缘顶部:20px;
}
.alertContainer{
宽度:400px;
保证金:自动;
边缘顶部:20px;
}
.signupFormContainer{
填充:20px 50px 20px 50px;
保证金:自动;
}
记事本应用程序
记事本应用程序
错误您的表单上有错误:
用户名
请输入用户名
密码
请输入密码
电子邮件地址
请输入有效的电子邮件地址
上传文本文件
请选择一个txt文件
注册
函数clearClass(){
$(“#用户名”).removeClass(“无效”);
$(“#密码”).removeClass(“无效”);
$(“#email”).removeClass(“无效”);
$(“#上载的_文件”).removeClass(“无效”);
}
$(“#注册”)。单击(函数(){
clearClass();
var frmError=假;
//如果($(“#用户名”).val()=“”){
//$(“#用户名”).addClass(“无效”);
//frmError=真;
// }
//如果($(“#密码”).val()=“”){
//$(“#密码”).addClass(“无效”);
//frmError=真;
// }
//如果($(“#电子邮件”).val()=“”){
//$(“#email”).addClass(“无效”);
//frmError=真;
// }
//if($(“#上传的_文件”).val()=“”){
//$(“#上载的_文件”).addClass(“无效”);
//frmError=真;
// }
如果(!frmError){
$('#createUser')。提交();
}
});
如果通过HTTP POST上载了按文件名命名的文件,则返回TRUE。这有助于确保恶意用户没有试图诱使脚本处理它不应处理的文件,例如/etc/passwd
bool is_uploaded_file ( string $filename )
为了正常工作,函数is\u uploaded\u file()
需要像$\u FILES['userfile']['tmp\u name']
这样的参数-客户端机器上上载的文件名$\u FILES['userfile']['name']
不起作用。
您可以查看文档中的
如果通过HTTP POST上载了按文件名命名的文件,则返回TRUE。这有助于确保恶意用户没有试图诱使脚本处理它不应处理的文件,例如/etc/passwd
bool is_uploaded_file ( string $filename )
为了正常工作,函数is\u uploaded\u file()
需要像$\u FILES['userfile']['tmp\u name']
这样的参数-客户端机器上上载的文件名$\u FILES['userfile']['name']
不起作用。
你可以查看文档,看是否有对那篇文章不是100%确定,我需要知道我的文件是否存在。不管它是否有文件大小?尼尔,你解决了吗?还没有,我现在不在我的办公桌上。我不是100%确定那个帖子,我需要知道我的文件是否存在。不管它是否有文件大小?尼尔,你解决了吗?还没有,我现在不在我的办公桌上。Cheers code only答案通常可以通过添加一些解释性文本来改进,即使只是用文字来解释代码在做什么。@Jasonaler感谢您的反馈已经添加了一些解释性文本,代码only答案通常可以通过添加一些解释性文本来改进,即使只是用文字解释代码在做什么。@Jasonaler感谢您的反馈,已经添加了一些解释