Bagaimana saya bisa mengunggah lebih dari satu file sekaligus di php?

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 file

Contoh 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']);

Code language: HTML, XML (xml)_4 untuk membuat formulir unggah file

<?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 berikut

1) 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)6

require_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

1

Ketiga, tambahkan kode berikut ke file

├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads

1 untuk memvalidasi dan mengunggah banyak file

<?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 bekerja

1) 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)0

Perhatikan 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 file

Karena

├── 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 diunggah

Agar 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)_1

Dan tambahkan fungsi

├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads

_9 ini ke file

├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads

5

3) 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)_2

4) 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)_3

5) 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)_4

Jika 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)5

Perhatikan 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

Bagaimana cara mengunggah banyak file sekaligus menggunakan PHP?

Inilah yang perlu Anda lakukan. .
Nama input harus didefinisikan sebagai array i. e. nama="masukanNama[]"
Elemen input harus memiliki multiple="multiple" atau hanya multipel
Dalam file PHP Anda gunakan sintaks "$_FILES['inputName']['param'][index]"
Pastikan untuk mencari nama dan jalur file kosong, array mungkin berisi string kosong

Bagaimana cara mengunggah banyak gambar sekaligus di PHP?

Unggah Banyak File dalam PHP (unggah. .
Sertakan file konfigurasi database untuk menghubungkan dan memilih database MySQL
Dapatkan ekstensi file menggunakan fungsi pathinfo() di PHP dan periksa apakah pengguna hanya memilih file gambar
Unggah gambar ke server menggunakan fungsi move_uploaded_file() di PHP

Bagaimana cara mengunggah banyak file dalam PHP dan menyimpannya di folder?

Sertakan upload-script. php
Buat formulir dengan dua atribut
method=”post” enctype=”multipart/form-data”
Buat input file dengan tiga atribut
type=”file” name=”file_name[]” multiple
Buat tombol kirim dengan name="submit"

Postingan terbaru

LIHAT SEMUA