文件不会上传到数据库(php)

时间:2017-11-30 10:53:46

标签: php html

所以我一直在尝试保存上传到数据库中的文件的路径,但第65行出现错误。似乎问题在于变量$evidence。任何人都可以弄清楚错误是什么。谢谢

<?php

    $target_dir = "uploads/";
    $target_file =  $target_dir . '/' . basename($_FILES["evidence"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["evidence"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["evidence"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["evidence "]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["evidence "]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }

       if(isset($_POST['submit'])){

            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "extensed";

            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$conn) {
                die("Connection failed: " . mysqli_connect_error());
            }

            $unittitle = mysqli_real_escape_string($conn,$_POST['unittitle']);
            $unitnumber = mysqli_real_escape_string($conn,$_POST['unitnumber']);
            $cwdeadline = mysqli_real_escape_string($conn,$_POST['cwdeadline']);
            $reqdate = mysqli_real_escape_string($conn,$_POST['reqdate']);
            $reason = mysqli_real_escape_string($conn,$_POST['reason']);
            $evidence = mysqli_real_escape_string($conn,$_POST['evidence']);

            $sql = "INSERT INTO extension (unitTitle, unitNumber, deadline, request_date, reason, evidence)
            VALUES ( '".$unittitle."', '".$unitnumber."', '".$cwdeadline."', '".$reqdate."', '".$reason."', '".$evidence."')";

            if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
            mysqli_close($conn);
        }  
    ?>

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Extensed</title>

</head>


<body style="">


    <div>
        <nav class="navbar navbar-light navbar-expand-md navigation-clean-button" style="">
            <div class="container">
                <a class="navbar-brand" href="#"><img src="assets/img/logo.png"></a>
                <button class="navbar-toggler" data-toggle="collapse" data-target="#navcol-1"><span class="sr-only">Toggle navigation</span><span class="navbar-toggler-icon"></span></button>
                <div class="collapse navbar-collapse" id="navcol-1">
                    <ul class="nav navbar-nav mr-auto"></ul><span class="navbar-text actions"> <a class="text-uppercase login" href="my_extensions.php" style="">My Extensions</a><a class="btn btn-outline-light btn-lg action-button" role="button" href="login.php" style="">LOGOUT </a></span></div>
            </div>
        </nav>
    </div>
    <div class="container">
        <div class="jumbotron" style="">
            <h1>Extension Request</h1>
            <p>Complete the form to make a new request.</p>



            <div class="error-message"></p></div>
            <form action="request.php" method='post' enctype="multipart/form-data">
                <div class="form-group">
                    <label>Unit Title</label>
                    <input class="form-control form-control-lg" type="text" name="unittitle" required="" placeholder="Unit Title">
                </div>
                <div class="form-group">
                    <label>Unit Number</label>
                    <input class="form-control form-control-lg" type="text" name="unitnumber" required="" placeholder="Unit Number">
                </div>
                <div class="form-group">
                    <label>Coursework deadline</label>
                    <input class="form-control form-control-lg" type="date" name="cwdeadline" required="">
                </div>
                <div class="form-group">
                    <label>Request date</label>
                    <input class="form-control form-control-lg" type="date" name="reqdate" required="">
                </div>

                <div class="form-group">
                    <label>Reason </label>
                    <textarea class="form-control form-control-lg" cols="15" name="reason" required="" placeholder="Reason..."></textarea>
                </div>
                <label>Evidence </label>
                <div class="form-group">
                    <form action="request.php" method="post" enctype="multipart/form-data">
                    <input type="file" name="evidence">
                </div>
                <div class="form-group">
                    <button class="btn btn-primary btn-lg" type="submit" name="submit">Make Request</button>
                </div>
            </form>
        </div>
    </div>
    <div class="footer-dark">
        <footer>
            <div class="container">
                <div class="row">
                    <div class="col-sm-6 col-md-3 item">
                        <h3>Services</h3>enter code here
                        <ul>
                            <li><a href="#">Manage </a></li>
                            <li><a href="#">Extension Request</a></li>
                        </ul>
                    </div>
                    <div class="col-sm-6 col-md-3 item">
                        <h3>Contact </h3>
                        <ul>
                            <li><a href="contact_us.html">Contact us</a></li>
                        </ul>
                    </div>
                    <div class="col-md-6 item text">
                        <h3><img src="assets/img/logo.png"> </h3>
                        <p>Extension created in CITY COLLEGE - International Faculty of the University of Sheffield.</p>
                    </div>
                    <div class="col item social"><a href="#"><i class="icon ion-social-facebook"></i></a><a href="#"><i class="icon ion-social-twitter"></i></a><a href="#"><i class="icon ion-social-snapchat"></i></a><a href="#"><i class="icon ion-social-instagram"></i></a></div>
                </div>
                <p class="copyright">Extension © 2017</p>
            </div>
        </footer>
    </div>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

它会生成错误,因为您正在分配类似输入数据而不是

$_FILES["evidence"]["name"];

 //remove thisline  $evidence =mysqli_real_escape_string($conn,$_POST['evidence']);

你必须分配这样的证据

$target_file =  $target_dir . '/' . basename($_FILES["evidence"]["name"]);
$evidence=$target_file;

现在证据保持图像的地址所以dir。图像现在也是证据。现在它将被上传到数据库 你没有传递数据。 这是错误。 任何问题评论。

供参考,这是用于上传个人资料图片的php脚本,

    <?php
if(isset($_POST["submit"])) 
    {
        $target_dir = "profilepic/";
        $targetfile=$target_dir.basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($targetfile,PATHINFO_EXTENSION);

        if($imageFileType != "jpg" &&$imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "jpeg" && $imageFileType != "JPEG" && $imageFileType != "gif" && $imageFileType != "GIF" ) 
        {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;

        }
        if ($uploadOk == 0) 
        {
        echo "<small>Sorry, your file was not uploaded.</small>";
        }
         else 
         {
         if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetfile)) 
         {
           echo "<small>upload successful</small>";
           $imgdir="UPDATE user SET profilepic='$targetfile' WHERE username='$uname';";
           $con->query($imgdir);
           header("location:profile.php");

         }
         else
          echo "not uploaded";
         } 
    }
?>

用于上传多个文件

    <?php
if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       
        $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
        $desired_dir="user_data";
        if(empty($errors)==true){
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"user_data/".$file_name);
                 $status=1; 
            }else{                                  //rename the file if another one exist
                $new_dir="user_data/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
            if($status==1){
            mysql_query($query);            
            }
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
?>


<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple="" />
    <input type="submit"/>
</form>

答案 1 :(得分:-2)

 <div class="form-group">
     <!-- replace the following line by label for example -->
     <form action="request.php" method="post" enctype="multipart/form-data">
     <input type="file" name="evidence">
</div>