The secret to wholesale...

In the article about buying Thai goods, I introduced wholesale markets and cheap...

Restaurant business and the...

Customers are a special figure in restaurants. This may seem obvious, but...

Guide the clues to...

Japanese goods Known for putting quality first. However, in the past, due...

Online cosmetics business and...

Starting a business, especially online cosmetics business is the silver trend and...
HomeLập trìnhPhpBạn đang cố...

Bạn đang cố gắng để dropzone.js hoạt động với PHP? – JavaScript


Xin chào :slight_smile:

Không biết có ai đã sử dụng dropzone.js ở đây chưa… Tôi đang cố gắng làm cho nó hoạt động (nghĩa là chèn thành công thông tin tệp vào cơ sở dữ liệu) nhưng lỗi này khiến tôi dừng lại khi tôi nhấn nút gửi:

Notice: Undefined index: myFile in C:\xampp...............\upload.php on line 8 cũng như lỗi của tôi “Format not allowed!

Hoặc nếu có thể, bạn có thể đề xuất một “dropzone” tốt hơn (mã nguồn mở) phổ biến hơn để bạn có thể dễ dàng tìm thấy hỗ trợ trên đó không?

Đây là mã (html):

<form action='upload.php' class="dropzone" id="videoDropzone" name="videoDropzone" method="post" enctype="multipart/form-data">
	 <p class="dz-message">Drop video here to upload</p>
	<select form="videoDropzone" name="saveTo" class="saveTo" required>
		<option value="public">Public (everyone)</option>
		<option value="private">Private (just me)</option>
		<option value="scheduled">Scheduled</option>
		<option value="unlisted">Unlisted</option>
	</select>
	<button type="submit" name="videoDropzoneSubmit" id="videoDropzoneSubmit">Save</button>
</form>

Và sau đây là cấu hình:

Dropzone.options.videoDropzone = {
  paramName: "myFile", // The name that will be used to transfer the file
  maxFilesize: 500, // MB
  maxFiles: 4,
  acceptedFiles: ".mp4, .jpg",
  addRemoveLinks: true,
  // Prevents Dropzone from uploading dropped files immediately
  autoProcessQueue: false,
  init: function() {
    var submitButton = document.querySelector("#videoDropzoneSubmit")
        myDropzone = this; // closure

    submitButton.addEventListener("click", function() {
      myDropzone.processQueue(); // Tell Dropzone to process all queued files.
    });

  }

};

Đây là tệp upload.php hoàn chỉnh:

<?php
session_start();
include_once 'inc/dbh.inc.php';
$id = $_SESSION['id'];

if (isset($_POST['videoDropzoneSubmit'])) {

	$file = $_FILES['myFile'];

	$fileName = $_FILES['myFile']['name'];
	$fileTmpName = $_FILES['myFile']['tmp_name'];
	$fileSize = $_FILES['myFile']['size'];
	$fileType = $_FILES['myFile']['type'];
	$fileError = $_FILES['myFile']['error'];

	$fileExt = explode('.', $fileName);
	$fileActualExt = strtolower(end($fileExt));

	$allowed = array('mp4', 'jpg');

	if (in_array($fileActualExt, $allowed)) {
		if ($fileError === 0) {
			if ($fileSize < 500000000) { // 500 MB
				$fileNameNew = "video".$id.".".$fileActualExt;
				$fileDestination = 'uploads/'.$fileNameNew;
				move_uploaded_file($fileTmpName, $fileDestination);
				$sql = "INSERT INTO uploads VALUES (null, '$id', '$fileNameNew', '$fileType', '$fileSize')";
				$result = mysqli_query($conn, $sql);
				header("Location:test.php?uploadsuccess");
			} else {
				echo "File too large!";
			}
		} else {
			echo "Error uploading your file!";
		}
	} else {
		echo "Format not allowed!";
	}

} else {
	header("Location: test.php");
}

Lưu ý rằng mã tải lên tương tự hoạt động với các dự án khác của tôi (không có dropzone). Tôi không thể tìm thấy tài liệu phù hợp cho các loại lỗi này.

Đọc thêm  Hướng dẫn đầy đủ về cửa hậu PHP cho WP_ (có thể gửi cho ai ...)



Zik.vn – Biên dịch & Biên soạn Lại

spot_img

Create a website from scratch

Just drag and drop elements in a page to get started with Newspaper Theme.

Buy Now ⟶

Bài viết liên quang

DMCA.com Protection Status