文件上传在 php 后端不工作。读取后找不到文件

我在 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'); 

    }

}

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


qq_遁去的一_1
浏览 198回答 1
1回答

至尊宝的传说

您的表单标签必须启用enctype="multipart/form-data"。试试这段代码。<!DOCTYPE html><html><body><form action="upload.php" method="post" enctype="multipart/form-data">&nbsp; Select image to upload:&nbsp; <input type="file" name="fileToUpload" id="fileToUpload">&nbsp; <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 imageif(isset($_POST["submit"])) {&nbsp; // Check if file already exists&nbsp; if (file_exists($target_file)) {&nbsp; &nbsp; echo "Sorry, file already exists.";&nbsp; &nbsp; $uploadOk = 0;&nbsp; }&nbsp; // Check file size&nbsp; if ($_FILES["fileToUpload"]["size"] > 500000) {&nbsp; &nbsp; echo "Sorry, your file is too large.";&nbsp; &nbsp; $uploadOk = 0;&nbsp; }&nbsp; // Allow certain file formats&nbsp; if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&nbsp; && $imageFileType != "gif" ) {&nbsp; &nbsp; echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";&nbsp; &nbsp; $uploadOk = 0;&nbsp; }&nbsp; // Check if $uploadOk is set to 0 by an error&nbsp; if ($uploadOk == 0) {&nbsp; &nbsp; echo "Sorry, your file was not uploaded.";&nbsp; // if everything is ok, try to upload file&nbsp; } else {&nbsp; &nbsp; if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {&nbsp; &nbsp; &nbsp; echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; echo "Sorry, there was an error uploading your file.";&nbsp; &nbsp; }&nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP