Hướng dẫn how can i upload more than one file at a time in php? - Làm cách nào để tải nhiều tệp lên cùng một lúc trong php?

Tôi muốn tải lên nhiều tệp và lưu trữ chúng trong một thư mục và lấy đường dẫn và lưu trữ nó trong cơ sở dữ liệu ... bất kỳ ví dụ tốt nào bạn tìm kiếm thực hiện tải lên nhiều tệp ...

Lưu ý: Tệp có thể thuộc bất kỳ loại nào ... Files can be of any type...

Hỏi ngày 24 tháng 4 năm 2010 lúc 11:42Apr 24, 2010 at 11:42

4

Tôi biết đây là một bài viết cũ nhưng một số giải thích thêm có thể hữu ích cho ai đó đang cố gắng tải lên nhiều tệp ... Đây là những gì bạn cần làm:

  • Tên đầu vào phải được định nghĩa là một mảng, tức là
    //$files = array_filter[$_FILES['upload']['name']]; //something like that to be used before processing files.
    
    // Count # of uploaded files in array
    $total = count[$_FILES['upload']['name']];
    
    // Loop through each file
    for[ $i=0 ; $i < $total ; $i++ ] {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a file path
      if [$tmpFilePath != ""]{
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if[move_uploaded_file[$tmpFilePath, $newFilePath]] {
    
          //Handle other code here
    
        }
      }
    }
    
    6
  • Phần tử đầu vào phải có
    //$files = array_filter[$_FILES['upload']['name']]; //something like that to be used before processing files.
    
    // Count # of uploaded files in array
    $total = count[$_FILES['upload']['name']];
    
    // Loop through each file
    for[ $i=0 ; $i < $total ; $i++ ] {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a file path
      if [$tmpFilePath != ""]{
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if[move_uploaded_file[$tmpFilePath, $newFilePath]] {
    
          //Handle other code here
    
        }
      }
    }
    
    7 hoặc chỉ
    //$files = array_filter[$_FILES['upload']['name']]; //something like that to be used before processing files.
    
    // Count # of uploaded files in array
    $total = count[$_FILES['upload']['name']];
    
    // Loop through each file
    for[ $i=0 ; $i < $total ; $i++ ] {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a file path
      if [$tmpFilePath != ""]{
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if[move_uploaded_file[$tmpFilePath, $newFilePath]] {
    
          //Handle other code here
    
        }
      }
    }
    
    8
  • Trong tệp PHP của bạn, hãy sử dụng cú pháp
    //$files = array_filter[$_FILES['upload']['name']]; //something like that to be used before processing files.
    
    // Count # of uploaded files in array
    $total = count[$_FILES['upload']['name']];
    
    // Loop through each file
    for[ $i=0 ; $i < $total ; $i++ ] {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a file path
      if [$tmpFilePath != ""]{
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if[move_uploaded_file[$tmpFilePath, $newFilePath]] {
    
          //Handle other code here
    
        }
      }
    }
    
    9
  • Đảm bảo tìm kiếm tên tệp và đường dẫn trống, mảng có thể chứa các chuỗi trống. Sử dụng
    
    Upload
    
    
    
    0 trước khi đếm.empty file names and paths, the array might contain empty strings. Use
    
    Upload
    
    
    
    0 before count.

Đây là một ví dụ về Down và bẩn [chỉ hiển thị mã có liên quan]

HTML:


PHP:

//$files = array_filter[$_FILES['upload']['name']]; //something like that to be used before processing files.

// Count # of uploaded files in array
$total = count[$_FILES['upload']['name']];

// Loop through each file
for[ $i=0 ; $i < $total ; $i++ ] {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a file path
  if [$tmpFilePath != ""]{
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if[move_uploaded_file[$tmpFilePath, $newFilePath]] {

      //Handle other code here

    }
  }
}

Hy vọng điều này sẽ giúp ích!

Ata

3.2005 huy hiệu vàng19 Huy hiệu bạc31 Huy hiệu đồng5 gold badges19 silver badges31 bronze badges

Đã trả lời ngày 17 tháng 8 năm 2012 lúc 13:11Aug 17, 2012 at 13:11

Andy Brahamandy BrahamAndy Braham

9.1694 Huy hiệu vàng44 Huy hiệu bạc54 Huy hiệu đồng4 gold badges44 silver badges54 bronze badges

15

Có thể chọn nhiều tệp và sau đó được tải lên bằng cách sử dụng tập lệnh PHP mẫu thực hiện tải lên:


Upload


1
The sample php script that does the uploading:


Upload


Các tệp đã chọn được nhận dưới dạng một mảng với


Upload


2 Lưu trữ tên của tệp đầu tiên.

Upload


3 Lưu trữ tên của tệp thứ hai. và như thế.

Upload


3 storing the name of second file.
and so on.

Đã trả lời ngày 18 tháng 12 năm 2011 lúc 8:37Dec 18, 2011 at 8:37

rjvrjvrjv

5.7194 Huy hiệu vàng29 Huy hiệu bạc48 Huy hiệu đồng4 gold badges29 silver badges48 bronze badges

1

Kịch bản đơn giản này đã làm việc cho tôi.


Đã trả lời ngày 14 tháng 8 năm 2017 lúc 15:36Aug 14, 2017 at 15:36

Rohit Mandiwalrohit MandiwalRohit Mandiwal

10.1k5 Huy hiệu vàng69 Huy hiệu bạc80 Huy hiệu Đồng5 gold badges69 silver badges80 bronze badges

HTML

  1. tạo Div với

    
    Upload
    
    
    
    4;

  2. tạo

    
    Upload
    
    
    
    5;

  3. 
    Upload
    
    
    
    6 của chức năng gọi nút đó
    
    Upload
    
    
    
    7

JavaScript

function  add_more[] {
  var txt = "
"; document.getElementById["dvFile"].innerHTML += txt; }

PHP

if[count[$_FILES["item_file"]['name']]>0]
 { 
//check if any file uploaded
 $GLOBALS['msg'] = ""; //initiate the global message
  for[$j=0; $j < count[$_FILES["item_file"]['name']]; $j++]
 { //loop the uploaded file array
   $filen = $_FILES["item_file"]['name']["$j"]; //file name
   $path = 'uploads/'.$filen; //generate the destination path
   if[move_uploaded_file[$_FILES["item_file"]['tmp_name']["$j"],$path]] 
{
   //upload the file
    $GLOBALS['msg'] .= "File# ".[$j+1]." [$filen] uploaded successfully
"; //Success message } } } else { $GLOBALS['msg'] = "No files found to upload"; //No file upload message }

Bằng cách này, bạn có thể thêm tệp/hình ảnh, như nhiều yêu cầu và xử lý chúng thông qua tập lệnh PHP.

bool.dev

17.4K5 Huy hiệu vàng65 Huy hiệu bạc92 Huy hiệu Đồng5 gold badges65 silver badges92 bronze badges

Đã trả lời ngày 1 tháng 10 năm 2012 lúc 6:41Oct 1, 2012 at 6:41

Kdniazikdniazikdniazi

771 Huy hiệu bạc1 Huy hiệu đồng1 silver badge1 bronze badge

4

Nó không khác với việc tải lên một tệp -


Upload


8 là một mảng chứa bất kỳ và tất cả các tệp được tải lên.

Có một chương trong hướng dẫn sử dụng PHP: tải lên nhiều tệp

Nếu bạn muốn bật nhiều tải lên tệp với lựa chọn dễ dàng ở đầu của người dùng [chọn nhiều tệp cùng một lúc thay vì điền vào các trường tải lên], hãy xem SWFupLoad. Nó hoạt động khác với biểu mẫu tải lên tệp bình thường và yêu cầu flash để hoạt động. SWFupload đã bị lỗi thời cùng với Flash. Kiểm tra các câu trả lời khác, mới hơn cho cách tiếp cận chính xác.

Đã trả lời ngày 24 tháng 4 năm 2010 lúc 11:45Apr 24, 2010 at 11:45

PekkapekkaPekka

435K138 Huy hiệu vàng966 Huy hiệu bạc1080 Huy hiệu Đồng138 gold badges966 silver badges1080 bronze badges

2





Untitled Document



Bài Viết Liên Quan

Chủ Đề