亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

文件上傳在 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 條件時,我得到“找不到文件”?,F在有點被難住了,因為我過去讓它工作過。我什至嘗試過像這樣的基本 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 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號