【问题标题】:File upload not working in the php back-end. No file being found once I read it文件上传在 php 后端不起作用。阅读后找不到文件
【发布时间】:2020-05-08 14:46:44
【问题描述】:

我在 php 上制作了一个表单,并在其中添加了一个图像输入选项。

<form action="./assets/actions/gallery_post.php" id="upload-form" method="POST">
            <input type="text" name="title" placeholder="Image title..." class="form-control" required>
            <br>
            <input type="text" name="description" placeholder="Image description..." class="form-control">
            <br>
            <input type="file" name="file", class="form-control" required>
            <br>
            <button type="submit" name="picture-submit" class="form-control submit">Upload Photo</button>
        </form>

以下是我上传文件的 php 代码。

// Check if the button was pressed
if (isset($POST['picture-submit'])) {
    echo "Entered"; 
    // Get the inputs
    $newfilename =  'gallery';
    $title = $_POST['title'];
    $description = $_POST['description'];

    $file = $_FILES['file'];
    if($file){
        echo "FILE";
    }else{
        echo "No File found";
    }
    console.log($file);

    // Obtaining some file information
    $fileName = $file['name'];
    $fileTmpName = $file['tmp_name'];
    $fileError = $file['error'];
    $fileType = $file['type'];

    // Checking file extensions
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
    echo "$fileActualExt";

    // Allowed extensions
    $allowed = array('jpeg', 'jpg', 'png', 'JPG');

    // if extension is allowed 
    if(in_array($fileActualExt, $allowed)){
        // check if any error
        if($fileError === 0){
                // Creating a unique file name
                $fileNew = $newfilename. "." . uniqid('', true) . "." . $fileActualExt;
                $fileDest = "assets/images/gallery/" . $fileNew;

                // Function to upload file
                move_uploaded_file($fileTmpName, $fileDest);

                // Making a database connection
                include_once ('dbh.php'); 

                $stmt = mysqli_stmt_init($conn);

                // Move to the database as well using prepared statements
                $sql = "INSERT into gallery(location, title, description) VALUES(?, ?, ?)";
                echo $sql;

                // Binding parameters
                mysqli_stmt_bind_param($stmt, "sss", $fileDest, $title, $description);
                mysqli_stmt_execute("$stmt");

                // Perform a query, check for error
                mysqli_query($conn, $sql) or die(mysqli_error($conn));
                echo "Query Sent";

                header("Location: ../gallery.php?upload=sucess");

        }else {
            echo "There was an error in file upload.";
        }
    }else {
        echo "You cannnot upload files of this type!";
    }
}

在上述场景中,它甚至没有进入 if 条件。当我删除 if 条件时,我得到“找不到文件”。现在有点难住了,因为我过去已经让它工作了。我什至尝试过像mysqli_query($conn, $sql) or die(mysqli_error($conn)); 这样的基本 HTML 查询格式,但这也不起作用。

【问题讨论】:

  • 第一个条件中至少有$_POST
  • 您是否尝试删除name="file", 中的,
  • 这是一个愚蠢的错误@Tuckbros 感谢您的帮助。

标签: php mysql forms request


【解决方案1】:

您的表单标签必须启用enctype="multipart/form-data"

试试这个代码。

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
  }
  // Check file size
  if ($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"], $target_file)) {
      echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }
}

?>

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多