Hướng dẫn how to upload multiple pdf file in mysql database using php? - cách tải lên nhiều tệp pdf trong cơ sở dữ liệu mysql bằng php?

Trong bài viết này, bạn sẽ tìm hiểu cách tải lên nhiều tệp và lưu trữ chúng trong cơ sở dữ liệu MySQL bằng PHP. Rất dễ dàng để tải lên một tệp và lưu trữ nó trong cơ sở dữ liệu, nhưng đôi khi cũng cần phải tải lên và lưu trữ nhiều tệp, như tải lên nhiều hình ảnh, pdf, tài liệu, v.v. Nhiều tệp tải lên cho phép người dùng chọn nhiều tệp cùng một lúc và tải tất cả các tệp lên máy chủ. Tải lên nhiều tệp là chức năng được sử dụng phổ biến nhất cho ứng dụng web.MySQL database using PHP. It is very easy to upload a single file and store it in the database, but sometimes there is also a need to upload and store multiple files, like uploading multiple images, PDF, docs and so on. Multiple file upload allows the user to choose multiple files at once and upload all files to the server. Uploading multiple files is the most commonly used functionality for the web application.

Trước tiên, hãy tạo biểu mẫu HTML để tải lên nhiều tệp và lưu trữ chúng trong cơ sở dữ liệu MySQL. Các thuộc tính biểu mẫu acstype = 'multipart/form-data' cho phép các tệp được gửi qua bài đăng.enctype='multipart/form-data' form attributes allow files to be sent through post.

Cần phải ghi 'Nhiều' trong đầu vào tệp để chọn và tải lên nhiều tệp. Ở đây, chúng tôi đã sử dụng một mảng tệp trong tên đầu vào để gửi nhiều tệp trong bài đăng.multiple' in file input to select and upload multiple files. Here, we have used a file array in the input name to send multiple files in the post.

<form method='post' action='#' enctype='multipart/form-data'>
<div class="form-group">
 <input type="file" name="file[]" multiple>
</div> 
<div class="form-group"> 
 <input type='submit' name='submit' value='Upload' class="btn btn-primary">
</div> 
</form>

Tiếp theo, tạo một cơ sở dữ liệu để lưu trữ các tập tin. Bạn có thể sao chép và dán câu lệnh này tạo ra câu lệnh vào cơ sở dữ liệu của bạn hoặc sử dụng bản hiện tại của bạn.

CREATE TABLE `files` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `uploaded_on` datetime NOT NULL,
 `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Tiếp theo, chúng tôi đã viết mã kết nối cơ sở dữ liệu. Đảm bảo thay thế 'tên máy chủ', 'tên người dùng', 'mật khẩu' và 'cơ sở dữ liệu' bằng thông tin và tên cơ sở dữ liệu của bạn.hostname', 'username', 'password' and 'database' with your database credentials and name.

$conn = mysqli_connect('hostname', 'username', 'password', 'database');
//Check for connection error
if($conn->connect_error){
  die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);    
}

Tiếp theo, chúng tôi có mã bằng văn bản để kiểm tra các tệp đã gửi, lưu tệp trong thư mục cục bộ và lưu trữ đường dẫn tệp được tải lên trong cơ sở dữ liệu. Hàm di chuyển_uploaded_file () của php tải hình ảnh lên máy chủ.move_uploaded_file() function of PHP uploads images to the server.

if(isset($_POST['submit'])){
// Count total uploaded files
$totalfiles = count($_FILES['file']['name']);

// Looping over all files
for($i=0;$i<$totalfiles;$i++){
$filename = $_FILES['file']['name'][$i];
 
// Upload files and store in database
if(move_uploaded_file($_FILES["file"]["tmp_name"][$i],'upload/'.$filename)){
// Image db insert sql
 $insert = "INSERT into files(file_name,uploaded_on,status) values('$filename',now(),1)";
 if(mysqli_query($conn, $insert)){
  echo 'Data inserted successfully';
 }
 else{
  echo 'Error: '.mysqli_error($conn);
 }
}else{
  echo 'Error in uploading file - '.$_FILES['file']['name'][$i].'<br/>';
} 
}
} 

Hoàn thành tập lệnh

Ở đây, chúng tôi đã hợp nhất các mã trên để tải nhiều tệp lên cơ sở dữ liệu.

<?php 

//Database Connection
$conn = mysqli_connect('hostname', 'username', 'password', 'database');
//Check for connection error
if($conn->connect_error){
  die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);    
}

if(isset($_POST['submit'])){
 // Count total uploaded files
 $totalfiles = count($_FILES['file']['name']);

 // Looping over all files
 for($i=0;$i<$totalfiles;$i++){
 $filename = $_FILES['file']['name'][$i];
 
// Upload files and store in database
if(move_uploaded_file($_FILES["file"]["tmp_name"][$i],'upload/'.$filename)){
		// Image db insert sql
		$insert = "INSERT into files(file_name,uploaded_on,status) values('$filename',now(),1)";
		if(mysqli_query($conn, $insert)){
		  echo 'Data inserted successfully';
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
	}else{
		echo 'Error in uploading file - '.$_FILES['file']['name'][$i].'<br/>';
	}
 
 }
} 
?>
<html>
<head>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
	<h2>Select Files to Upload</h2>
	<form method='post' action='#' enctype='multipart/form-data'>
	<div class="form-group">
	 <input type="file" name="file[]" id="file" multiple>
	</div> 
	<div class="form-group"> 
	 <input type='submit' name='submit' value='Upload' class="btn btn-primary">
	</div> 
	</form>
</div>	
</body>
</html>

Hướng dẫn how to upload multiple pdf file in mysql database using php? - cách tải lên nhiều tệp pdf trong cơ sở dữ liệu mysql bằng php?

Những bài viết liên quan

Php vệ sinh đầu vào cho chuỗi báo giá ngẫu nhiên mysqlphp Trích dẫn chuỗi tạo ra phần trăm tính toán tỷ lệ phần trăm của bản sửa lỗi TotalPhp: đối số không hợp lệ được cung cấp cho các tệp khóa foreach với flock () Hiển thị tệp pdf từ cơ sở dữ liệu SPLFILEOBject ví dụ Cách tải lên một tệp trong phpsimple php email biểu mẫu thiết lập lại hệ thống thiết lập lại trong xác thực phphtt Xác thực trong chèn php trong cơ sở dữ liệu mà không cần trang làm mới php
PHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
PHP Display PDF file from Database
How to read CSV file in PHP and store in MySQL
Generating word documents with PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Simple PHP email form
Password reset system in PHP
HTTP authentication with PHP
PHP file cache library
PHP get current directory url
How to prevent CSRF attack in PHP
Forgot Password Script PHP mysqli database
PHP Contact Form with Google reCAPTCHA
HTML Form Validation in PHP
Insert in database without page refresh PHP

Làm thế nào tôi có thể tải lên nhiều hình ảnh trong PHP và lưu trữ trong cơ sở dữ liệu và thư mục?

Tải lên nhiều tệp trong PHP (Tải lên ...
Bao gồm tệp cấu hình cơ sở dữ liệu để kết nối và chọn cơ sở dữ liệu MySQL ..
Nhận tiện ích mở rộng tệp bằng hàm pathInfo () trong PHP và kiểm tra xem người dùng chỉ chọn các tệp hình ảnh ..
Tải hình ảnh lên máy chủ bằng hàm di chuyển_uploaded_file () trong PHP ..

Làm thế nào tôi có thể thêm nhiều dữ liệu và một lần trong PHP và MySQL?

Người ta cũng có thể chèn nhiều hàng vào bảng với một truy vấn chèn cùng một lúc.Để thực hiện điều này, bao gồm nhiều danh sách các giá trị cột trong phần chèn vào câu lệnh, trong đó các giá trị cột cho mỗi hàng phải được đặt trong dấu ngoặc đơn và được phân tách bằng dấu phẩy.include multiple lists of column values within the INSERT INTO statement, where column values for each row must be enclosed within parentheses and separated by a comma.

Chúng ta có thể tải lên PDF trong MySQL không?

MySQL có kiểu dữ liệu blob có thể được sử dụng để lưu trữ các tệp như .pdf, .jpg, .txt và tương tự. pdf, . jpg, . txt, and the like.

Làm thế nào hiển thị nhiều hình ảnh từ cơ sở dữ liệu MySQL trong PHP?

Trong tập lệnh ví dụ, chúng tôi sẽ triển khai một hệ thống quản lý bộ sưu tập với nhiều hình ảnh bằng PHP và MySQL ...
Lấy thông tin thư viện từ cơ sở dữ liệu và liệt kê trên trang web ..
Tải nhiều hình ảnh lên máy chủ và thêm dữ liệu biểu mẫu vào cơ sở dữ liệu ..
Xem bộ sưu tập với nhiều hình ảnh ..
Chỉnh sửa và cập nhật nhiều hình ảnh ..