Hướng dẫn how can i store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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

Hướng dẫn how can i store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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.1194 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp trong php?

rjvrjvrjv

5.6594 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp trong php?

PekkapekkaPekka

435K137 Huy hiệu vàng966 Huy hiệu bạc1079 Huy hiệu Đồng137 gold badges966 silver badges1079 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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.4K52 Huy hiệu vàng73 Huy hiệu bạc113 Huy hiệu đồng52 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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 store multiple files in php? - làm thế nào tôi có thể lưu trữ nhiều tệp 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 cách nào để lưu nhiều tệp trong PHP?

Dưới đâ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à name = "inputName []".
Phần tử đầu vào phải có nhiều = "nhiều" hoặc chỉ nhiều ..
Trong tệp PHP của bạn, hãy sử dụng cú pháp "$ _files ['inputName'] ['param'] [index]".
Đả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 ..

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ó giá trị ID, hình ảnh và 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 cách nào để lưu trữ nhiều tệp trong cơ sở dữ liệu?

Tải lên nhiều tệp và lưu trữ trong cơ sở dữ liệu MySQL bằng PHP..
Tạo một cơ sở dữ liệu & bảng MySQL. Tôi đã tạo một CodingStatus cơ sở dữ liệu và kết nối với PHP bằng truy vấn sau. ....
Tạo một biểu mẫu để tải lên nhiều tệp. ....
Viết mã PHP để tải lên nhiều trong cơ sở dữ liệu MySQL. ....
Hiển thị nhiều tệp trên trang web ..

Làm thế nào để bạn lưu trữ các tệp trong PHP?

Tải lên tệp PHP..
Định cấu hình tệp "php.ini". Đầu tiên, đảm bảo rằng PHP được cấu hình để cho phép tải lên tệp. ....
Kiểm tra nếu tệp đã tồn tại. Bây giờ chúng ta có thể thêm một số hạn chế. ....
Giới hạn kích thước tệp.Trường đầu vào tệp trong biểu mẫu HTML của chúng tôi ở trên được đặt tên là "Filetoupload".....
Giới hạn loại tệp.....
Hoàn thành Tải lên Tập lệnh Php ..

Làm thế nào tôi có thể di chuyển nhiều tải lên trong PHP?

Làm thế nào để tải lên nhiều tệp và lưu trữ chúng trong một thư mục với PHP ?..
Tên đầu vào phải được định nghĩa là một mảng tức là name = "inputName []".
Phần tử đầu vào phải có nhiều = "nhiều" hoặc chỉ nhiều ..
Trong tệp PHP, sử dụng cú pháp "$ _FILES ['inputName'] ['param'] [index]".

Làm thế nào tôi có thể lưu trữ nhiều hình ảnh trong PHP?

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