为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
qq_遁去的一_1 2023-03-04 10:49:10
我在 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 pressedif (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));但这也不起作用。
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

您的表单标签必须启用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.";

    }

  }

}


?>


查看完整回答
反对 回复 2023-03-04
  • 1 回答
  • 0 关注
  • 102 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信