PHP表单验证上传的文件

时间:2018-04-18 14:38:56

标签: php forms validation

我在php中验证文件上传时遇到了问题。基本上代码没有看到文件是空的,我整个下午都在看它并且无法弄明白。我确定它很简单。如果有人能解决我的问题,我会非常感激。在此先感谢.....

PS。这是一个未完成的项目,我只是停留在验证阶段。

<?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>

1 个答案:

答案 0 :(得分:2)

UINavigationController B

如果文件名命名的文件是通过HTTP POST上传的,则返回TRUE。这有助于确保恶意用户没有尝试欺骗脚本处理不应该工作的文件 - 例如/ etc / passwd。

transitioningDelegate

为了正常工作,if(!file_exists($_FILES['uploaded_file']['tmp_name']) || !is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) { echo 'No upload'; } 需要像bool is_uploaded_file ( string $filename ) 这样的参数, - 客户端计算机上function is_uploaded_file()上传文件的名称不起作用。 您可以查看mroe

的文档
相关问题