Tôi không biết cách sửa đổi đoạn mã sau để làm cho nó tải lên nhiều tệp bằng cách chọn một lần. Theo đoạn mã sau, tôi có thể tải lên từng tệp một. Chỉ muốn sửa đổi nó vì thông qua mã này, tôi không chỉ tải tệp lên mà còn hiển thị tất cả các tệp đã tải lên, trên cùng một trang để tải xuống và xóa
Brief upload query("INSERT INTO upload (name,date) VALUES ('$name','$date')"); if($query){ header("location:index.php"); } else{ die(mysql_error()); } } ?>Brief Uploading System
SUBMIT HERE
ID FILE NAME Date Download Remove
- stackoverflow. comghi bàn. 0
Bạn có thể tải lên nhiều tệp theo cách này
Trường đầu vào phải được xác định là một mảng i. e. <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="" /> <input type="submit"/> </form> 1
Nó nên được định nghĩa là <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="" /> <input type="submit"/> </form> 2
// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } }
Để biết thêm chi tiết Tải lên nhiều PHP
Thêm câu hỏi với thẻ tương tự
Trong hướng dẫn này, chúng tôi sẽ tạo tập lệnh PHP Tải lên nhiều tệp có xác minh phần mở rộng và kích thước tệp, để thực hiện tải lên an toàn và lưu thông tin tệp vào cơ sở dữ liệu MySQL
Đối với hướng dẫn này, chúng tôi sẽ tạo một hệ thống tải lên hình ảnh. Điều này có thể được sử dụng để tải lên hình ảnh, PDF, Tài liệu, nhật ký hoặc bất kỳ loại tệp nào, đảm bảo bạn thay đổi tập lệnh
Nếu chưa quen với việc tải tệp lên, bạn có thể xem bài viết của chúng tôi về Tải lên tệp đơn giản bằng PHP, để bắt đầu với cơ bản
HTML
Đây là một diễn đàn foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 1 đơn giản, sẽ không có bất kỳ phong cách nào, vì chúng tôi đang tập trung vào việc tải lên PHP
<form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="" /> <input type="submit"/> </form>Đảm bảo thêm foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 2 ,______23 và quan trọng nhất là foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 4 để có thể chọn nhiều tệp
PHP
Chuyển sang mã PHP. chúng ta có thể bắt đầu với foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 5 trong điều kiện if để đảm bảo một số tệp được chọn và chúng ta sẵn sàng tải lên
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 6 sẽ chứa thông tin của từng tệp, nhưng khi chọn thêm một tệp đó, nó sẽ có thông tin chi tiết của từng tệp được đặt bên trong một mảng khác
Để xem cách dữ liệu được lưu trữ trong mảng, hãy tham khảo bài viết của chúng tôi về Tải lên tệp đơn giản với PHP,
Để truy cập chúng, chúng tôi sẽ sử dụng vòng lặp foreach foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 7
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; }Bây giờ để thực hiện phần xác minh. Đầu tiên chúng ta sẽ tạo một mảng với các phần mở rộng được phép trong đó
// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 1Vì chúng tôi đang thực hiện tải lên hình ảnh, chúng tôi chỉ cần cho phép các tiện ích mở rộng hình ảnh i. e. JPEG, PNG. vân vân. Bạn có thể thêm các tiện ích mở rộng phù hợp mà bạn cần. Để lấy phần mở rộng của tệp đã tải lên, chúng tôi sẽ sử dụng tên tệp không phải Loại tệp, để lấy phần mở rộng, chúng tôi sẽ sử dụng PHP foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 8 & foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 9
Các tiện ích mở rộng cũng có thể ở dạng chữ HOA hoặc chữ HOA để khắc phục sự cố, chúng tôi sẽ chuyển chúng thành chữ thường hoặc chữ hoa như bạn đã đề cập trong mảng // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 10
Sử dụng PHP // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 11 để xác minh xem tệp tải lên có được phép hay không, gần xong rồi
// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 6Để giới hạn kích thước tệp tải lên, // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 12 sẽ được sử dụng để lấy kích thước tệp
// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 8SQL
Đó là tất cả với phần tải lên, các tệp đã sẵn sàng để được chuyển vào một thư mục và lưu trữ các chi tiết tệp trong bảng MySQL nếu cần. Trong hướng dẫn này, chúng tôi sử dụng bảng MySQL có 4 hàng
// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 9// Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 13 là duy nhất cho mỗi người dùng nếu bạn có ứng dụng dựa trên người dùng hoặc bất kỳ ID duy nhất nào có thể xác định từng mục
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 1Di chuyển tệp
Tệp đã tải lên sẽ được phân bổ không gian ở vị trí tạm thời như đã đề cập trong php. ban đầu. Cần di chuyển tệp từ vị trí tạm thời sang vị trí khác để sử dụng lại. Bạn có thể di chuyển tệp đến một vị trí khác bằng cách sử dụng // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 14, ở đây chúng tôi sẽ di chuyển tệp đến thư mục // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 15, đảm bảo thư mục tồn tại, vì // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 14 không thể tạo thư mục. Vì vậy, nên xác minh sự tồn tại của thư mục
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 5Nếu bạn định tạo thư mục cho từng người dùng, bạn có thể sử dụng // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 17
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; } 7Để đổi tên tập tin
Người dùng có thể tạo sự cố nếu tồn tại tệp khác có cùng tên, vì // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 14 không thể di chuyển tệp nếu tồn tại tệp khác. Bạn có thể đổi tên tệp hoặc đổi tên tất cả các tệp đã tải lên bằng số sê-ri có thể ngăn chặn hoàn toàn sự cố này. Với // Count # of uploaded files in array $total = count($_FILES['images']['name']); // Loop through each file for( $i=0 ; $i < $total ; $i++ ) { //Get the temp file path $tmpFilePath = $_FILES['images']['tmp_name'][$i]; //Make sure we have a file path if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } } 19
Tên tệp OLD_FILE_PATH có phần mở rộng & vị trí và tên mới NEW_FILE_NAME
<form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="" /> <input type="submit"/> </form> 0Quanh co
Bạn có thể sử dụng các mã sau để tải lên các tệp có kích thước và loại bất kỳ, hãy đảm bảo rằng bạn có đủ dung lượng trên máy chủ. Mã hoàn chỉnh cho dự án