Dalam tutorial sebelumnya, Anda telah mempelajari cara mengunggah satu file dari klien ke server web di PHP. Semua aturan mengunggah satu file relevan untuk banyak file
Pertama, elemen formulir HTML harus memiliki atribut
<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)8 yang disetel ke<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)9 untuk mengaktifkan pengunggahan file. Sebagai contoh<form action="index.php" method="post" enctype="multipart/form-data">
Code language: PHP (php)Kedua, elemen input file harus memiliki atribut
<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_0 dan namanya harus memiliki tanda kurung siku (<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)1) seperti ini<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)Di PHP, Anda dapat mengakses
<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_2 untuk mendapatkan informasi dari file yang diunggah<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_3 adalah nama elemen input fileContoh unggah banyak file PHP
Contoh berikut menggunakan kembali fungsi dan logika yang sudah ada yang dikembangkan dalam tutorial unggah file
Pertama, buat struktur proyek berikut
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_Kedua, tambahkan kode berikut ke
<?php var_dump($_FILES['files']);
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_4 melakukan hal berikut1) Mulai sesi baru atau lanjutkan sesi yang ada
session_start();
2) Muat kode dari file
<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)6require_once __DIR__ . '/inc/flash.php';
Code language: PHP (php)3) Panggil fungsi
<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_7 untuk menampilkan pesan dengan nama<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)8. Fungsi<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)_7 didefinisikan dalam file├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
0<?php flash('upload') ?>
Code language: HTML, XML (xml)4) Buat formulir unggahan yang dikirimkan ke file
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
1Ketiga, tambahkan kode berikut ke file
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; require_once __DIR__ . '/inc/functions.php'; const ALLOWED_FILES = [ 'image/png' => 'png', 'image/jpeg' => 'jpg' ]; const MAX_SIZE = 5 * 1024 * 1024; // 5MB const UPLOAD_DIR = __DIR__ . '/uploads'; $is_post_request = strtolower($_SERVER['REQUEST_METHOD']) === 'post'; $has_files = isset($_FILES['files']); if (!$is_post_request || !$has_files) { redirect_with_message('Invalid file upload operation', FLASH_ERROR); } $files = $_FILES['files']; $file_count = count($files['name']); // validation $errors = []; for ($i = 0; $i < $file_count; $i++) { // get the uploaded file info $status = $files['error'][$i]; $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; // an error occurs if ($status !== UPLOAD_ERR_OK) { $errors[$filename] = MESSAGES[$status]; continue; } // validate the file size $filesize = filesize($tmp); if ($filesize > MAX_SIZE) { // construct an error message $message = sprintf("The file %s is %s which is greater than the allowed size %s", $filename, format_filesize($filesize), format_filesize(MAX_SIZE)); $errors[$filesize] = $message; continue; } // validate the file type if (!in_array(get_mime_type($tmp), array_keys(ALLOWED_FILES))) { $errors[$filename] = "The file $filename is allowed to upload"; } } if ($errors) { redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR); } // move the files for($i = 0; $i < $file_count; $i++) { $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; $mime_type = get_mime_type($tmp); // set the filename as the basename + extension $uploaded_file = pathinfo($filename, PATHINFO_FILENAME) . '.' . ALLOWED_FILES[$mime_type]; // new filepath $filepath = UPLOAD_DIR . '/' . $uploaded_file; // move the file to the upload dir $success = move_uploaded_file($tmp, $filepath); if(!$success) { $errors[$filename] = "The file $filename was failed to move."; } } $errors ? redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR) : redirect_with_message('All the files were uploaded successfully.', FLASH_SUCCESS);
Code language: HTML, XML (xml)Bagaimana
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_1 bekerja1) Mulai sesi baru atau lanjutkan sesi yang sudah ada
session_start();
2) Muat kode dari
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_0 dan├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
5<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)0Perhatikan bahwa Anda harus menggunakan
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_0 dari tutorial pesan flash dan├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
5 dari tutorial upload fileKarena
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_1 berurusan dengan banyak file, itu akan mengeluarkan beberapa pesan kesalahan, satu untuk setiap file yang diunggahAgar nyaman, kita dapat menentukan fungsi yang mengembalikan satu pesan kesalahan dari beberapa pesan kesalahan seperti ini
<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)_1Dan tambahkan fungsi
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
53) Kembalikan pesan kesalahan jika metode permintaan bukan
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)1 atau<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)2 tidak berisi bidang<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)3<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)_24) Dapatkan file yang diunggah dari
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)2 dan jumlah file yang diunggah<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)_35) Untuk setiap file yang diunggah, periksa kode kesalahan dan validasi ukuran file, ketik. Jika terjadi kesalahan, tambahkan pesan kesalahan ke larik
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)5, lewati putaran validasi saat ini, dan validasi file berikutnya<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)_4Jika terjadi kesalahan, arahkan kembali ke
<?php var_dump($_FILES['files']);
Code language: HTML, XML (xml)4 dan tampilkan pesan kesalahan<input type="file" name="files[]" id="files" multiple />
Code language: PHP (php)5Perhatikan bahwa kami menggunakan fungsi
├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads
_9 untuk mengonversi larik<?php session_start(); require_once __DIR__ . '/inc/flash.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>PHP upload multiple files</title> <link rel="stylesheet" href="//www.phptutorial.net/app/css/style.css" /> </head> <body> <?php flash('upload') ?> <main> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <label for="files">Select files to upload:</label> <input type="file" name="files[]" id="files" multiple required/> </div> <div> <button type="submit">Upload</button> </div> </form> </main> </body> </html>
Code language: HTML, XML (xml)5 menjadi pesan kesalahan berformat tunggal