簡體   English   中英

如何使用PHP上傳照片/文件到服務器

[英]How to upload photos/file to server using PHP

您好,我正在嘗試允許用戶將照片上傳到列表中。 我正在使用 dropzone.js 並完成了表單,它工作得很好! 除了照片實際上沒有上傳到文件夾“上傳/”

我已經嘗試了許多不同的方法,花了好幾天時間進行研究,但我終其一生都無法找出問題所在。

請注意,這是一個多頁表格,所以我不能在這里發布整個表格,但我會嘗試分享重要的腳本!

拖放區代碼:

  <head>
  <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
</head>

    <div class="container" >
        <div class='content'>
        <form action="create.php" method="post" type="file" name="file" class="dropzone" id="dropzonewidget" enctype="multipart/form-data">
       
    </div> 
  </div>
  </div>
    </div>

    <div class="row form-block flex-column flex-sm-row">
      <div class="col text-center text-sm-start"><a class="btn btn-link text-muted" href="user-add-2.php"> <i class="fa-chevron-left fa me-2"></i>Back</a>
      </div>
        <div class="col text-center text-sm-end"><button type="submit" name="save" class="btn btn-primary">save</button><i class="fa-chevron-right fa ms-2"></i></div>
      </form>

現在對於 PHP 腳本:我有 2 次接近的嘗試,第一次實際上進入數據庫,第二次只是回顯所有錯誤腳本

腳本 1:(有點作品)

 $uploadDir = 'var/www/uploads';
 $tmpFile = $_FILES['file']['tmp_name'];

 // upload file to directory
 $fileName = $uploadDir.'/'.time().'-'. $_FILES['file'] 
 ['name'];
 move_uploaded_file($tmpFile,$fileName); {

 
 $sql = "INSERT INTO hosts(id, user_name, name, type, 
 country, city, state, zip, about, price, photo)
  VALUES('$id', '$user_name', '$name', 
 '$type', '$country', '$city', '$state', '$zip', 
 '$about', '$price', '$fileName')";

腳本 2:

 $target_dir = "uploads/";
 $target_file = $target_dir . basename($_FILES["file"] 
 ["name"]);
 $uploadOk = 1;
 $imageFileType = 
 strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

 // Check if image file is a actual image or fake image

 $check = getimagesize($_FILES["file"]["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["file"]["size"] > 1000000) {
    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["file"]["tmp_name"], 
   $target_file)) {
   echo "The file ". htmlspecialchars( basename( 
   $_FILES["file"]["name"])). " has been uploaded.";
   //$pictureName = "uploads". basename( 
   $_FILES["fileToUpload"]["name"]);
   } else {
   echo "Sorry, there was an error uploading your 
   file.";
   }

在這兩次嘗試結束時,我有一個簡單的插入查詢(我知道 SQL 次注入,只是想讓它先工作。)

希望我沒有讓你頭疼

請注意,我是 PHP 和 SO 的新手,所以請多多關照,我希望更有經驗的開發人員可以幫助我度過這些麻煩的時刻,在此先感謝:如果您理解我的問題,請發布一些代碼而不是含糊的回復: ) 干杯

對於 dropzone 上傳(多個文件上傳)

一種。 不需要指定enctype="multipart/form-data"method="post" type="file" name="file"

b. 無需使用提交按鈕

c。如前所述,確保有一個名為uploads的子文件夾,並且它是允許寫入的

d. 要進行進一步的數據庫操作(例如插入),請在 php 中執行插入查詢,確保在構造插入查詢時使用$_FILES['file']['name']

因此,請將您的代碼修改為:

HTML

<head>
  <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
</head>

    <div class="container" >
        <div class='content'>
        <form action="create.php" class="dropzone" id="dropzonewidget">
       </form>
    </div> 
  </div>

創建.php

<?php
$target_dir = "./uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir.$_FILES['file']['name'])) {
   $status = 1;

// do further update in database
// please note that the file name is $_FILES['file']['name']
// make sure you use parametized prepared statement in your insert query
// to avoid SQL injection attacks
//
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM