Hướng dẫn how can i upload multiple files 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?

142

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

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

Hướng dẫn how can i upload multiple files 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?

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!

Hướng dẫn how can i upload multiple files 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?

Ata

3.1805 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.1494 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

Hướng dẫn how can i upload multiple files 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?

rjvrjvrjv

5.6794 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.

'; 
  move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]);
}

?>

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

Hướng dẫn how can i upload multiple files 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?

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

Hướng dẫn how can i upload multiple files 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?

PekkapekkaPekka

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

2





Untitled Document



";
echo "";
for($i=1; $i<=$max_no_img; $i++){
echo "";
}

echo "";
echo "
Images $i
"; while(list($key,$value) = each($_FILES['images']['name'])) { //echo $key; //echo "
"; //echo $value; //echo "
"; if(!empty($value)){ // this will check if any blank field is entered $filename =rand(1,100000).$value; // filename stores the value $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line $add = "upload/$filename"; // upload directory path is set //echo $_FILES['images']['type'][$key]; // uncomment this line if you want to display the file type //echo "
"; // Display a line break copy($_FILES['images']['tmp_name'][$key], $add); echo $add; // upload the file to the server chmod("$add",0777); // set permission to the file. } } ?>

Hướng dẫn how can i upload multiple files 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?

Prix

Huy hiệu vàng 19.3K1515 gold badges71 silver badges130 bronze badges

Đã trả lời ngày 5 tháng 2 năm 2011 lúc 20:38Feb 5, 2011 at 20:38

1

Đơn giản là, chỉ cần đếm mảng tệp trước, sau đó trong khi vòng lặp bạn có thể dễ dàng thực hiện điều này

$count = count($_FILES{'item_file']['name']);

Bây giờ bạn có tổng số tệp đúng.

Trong khi vòng lặp làm như thế này:

$i = 0;
while($i<$count)
{
    Upload one by one like we do normally
    $i++;
}

BENSIU

23.4K54 Huy hiệu vàng73 Huy hiệu bạc113 Huy hiệu đồng54 gold badges73 silver badges113 bronze badges

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

Biệt danhNick

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

1

Đây là một chức năng tôi đã viết, trong đó trả về một mảng


Upload


8 dễ hiểu hơn.

function getMultiple_FILES() {
    $_FILE = array();
    foreach($_FILES as $name => $file) {
        foreach($file as $property => $keys) {
            foreach($keys as $key => $value) {
                $_FILE[$name][$key][$property] = $value;
            }
        }
    }
    return $_FILE;
}

Đã trả lời ngày 23 tháng 5 năm 2014 lúc 5:46May 23, 2014 at 5:46

Hướng dẫn how can i upload multiple files 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?

CheejygcheejygCheejyg

3263 Huy hiệu bạc12 Huy hiệu Đồng3 silver badges12 bronze badges

Tôi chạy vòng lặp foreach với phần tử lỗi, trông giống như

//$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

    }
  }
}
0

Đã trả lời ngày 17 tháng 5 năm 2014 lúc 11:26May 17, 2014 at 11:26

Chúng tôi có thể dễ dàng tải lên nhiều tệp bằng PHP bằng cách sử dụng tập lệnh dưới đây.

Tải xuống mã nguồn đầy đủ và xem trướ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

    }
  }
}
1

Đã trả lời ngày 13 tháng 5 năm 2016 lúc 4:27May 13, 2016 at 4:27

Hướng dẫn how can i upload multiple files 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?

Merbin Joemerbin JoeMerbin Joe

5923 Huy hiệu bạc25 Huy hiệu Đồng3 silver badges25 bronze badges

//$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

    }
  }
}
2

Đã trả lời ngày 15 tháng 6 năm 2016 lúc 6:40Jun 15, 2016 at 6:40

Hướng dẫn how can i upload multiple files 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?

Vishnu Bhadoriyavishnu BhadoriyaVishnu Bhadoriya

1.6161 Huy hiệu vàng22 Huy hiệu bạc28 Huy hiệu đồng1 gold badge22 silver badges28 bronze badges

1

Đây là những gì làm việc cho tôi. Tôi đã phải tải lên các tệp, lưu trữ tên tệp và tôi đã có thêm inof từ các trường đầu vào để lưu trữ và một bản ghi cho mỗi tên tệp. Tôi đã sử dụng serialize () sau đó thêm nó vào truy vấn SQL chính.

//$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

    }
  }
}
3

Đã trả lời ngày 22 tháng 1 năm 2020 lúc 17:13Jan 22, 2020 at 17:13

//$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

    }
  }
}
4

Và bạn phải kiểm tra mã HTML của mình

//$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

    }
  }
}
5

Liên kết đẹp trên:

PHP đơn tải lên với sự giải thích cơ bản khác nhau..

Tải lên tệp PHP với xác thực

Tải lên nhiều tệp php với xác thực Bấm vào đây để tải về mã nguồn

Tải lên nhiều tệp php/jQuery lên với ProgressBar và xác thực (bấm vào đây để tải xuống mã nguồn)

Cách tải lên các tệp trong PHP và lưu trữ trong cơ sở dữ liệu MySQL (bấm vào đây để tải xuống mã nguồn)

Đã trả lời ngày 3 tháng 5 năm 2017 lúc 8:29May 3, 2017 at 8:29

Làm thế nào tôi có thể tải lên nhiều hơn một tệp tại một thời điểm trong PHP?

Sử dụng nhiều thẻ, một mảng trong thẻ tên của thuộc tính đầu vào trong biểu mẫu và hàm di chuyển_uploaded_file () để tải lên nhiều tệp trong PHP ..
Sử dụng PDO để tải lên nhiều tệp trong cơ sở dữ liệu trong PHP ..

Có bao nhiêu tệp có thể tải lên php?

Khi bạn đang làm việc với nhiều tệp tải lên trong PHP, cần phải tăng số lượng tệp tối đa để tải lên. Theo mặc định, bạn có thể tải lên 20 tệp thông qua một yêu cầu duy nhất. Nếu bạn muốn tải lên hơn 20 tệp, bạn cần phải sửa đổi giá trị biến MAX_FILE_UPLOADS trong PHP. Tệp INI.20 files via a single request. If you want to upload more than 20 files, you should need to modify the max_file_uploads variable value in php. ini file.

Làm thế nào chúng ta có thể tải lên nhiều tệp trong PHP và lưu trữ trong cơ sở dữ liệu?

Bạn có thể sử dụng MAMP hoặc XAMPP để tạo cơ sở dữ liệu và bảng để tải lên nhiều tệp trong PHP.Tạo cơ sở dữ liệu `Cơ sở dữ liệu_name`.Tạo `Table_Name` bên trong cơ sở dữ liệu.Chạy tập lệnh SQL sau đây từ bảng phpmyadmin để tạo bảng người dùng và nó sẽ có ID, hình ảnh và giá trị data_time trong cơ sở dữ liệu MySQL.use MAMP or XAMPP to create database and table for multiple file uploading in PHP. Create database `database_name`. Create `table_name` inside the database. Run the following SQL script from the PHPMyAdmin panel to create user tables, and it will have an id, images, and data_time values in the MySQL database.

Làm thế nào để bạn tải lên nhiều hơn một tệp?

Tải lên nhiều tập tin..
Duyệt đến trang nơi bạn muốn tải lên các tệp ..
Chuyển đến Chỉnh sửa> Thêm, sau đó chọn tab Tệp.....
Chọn Tải lên:.
Trên màn hình tải lên, chọn Duyệt/Chọn Tệp:.
Duyệt đến các tệp bạn muốn tải lên từ máy tính của mình và sử dụng CTRL/CMD +chọn để chọn nhiều tệp ..
Chọn Tải lên ..