Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Mengunggah gambar dengan bantuan array dan mengambil kesalahan

Ini akan bekerja untuk Anda

Contoh HTML

<form action="" enctype="multipart/form-data" method="post">

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <div>
        <input type="submit" value="Send">
    </div>
</form>

Kode PHP

$allowedExtention = array (
        'jpg',
        'jpeg',
        'png',
        'bmp',
        'tiff',
        'gif' 
);
$errors = array ();
$output = array ();

if (! empty ( $_FILES ['image'] ['tmp_name'] )) {

    foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {

        $fileName = $_FILES ['image'] ['name'] [$key];
        $fileSize = $_FILES ['image'] ['size'] [$key];
        $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];

        $fileExtention = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExtention = strtolower ( $fileExtention );

        if (! in_array ( $fileExtention, $allowedExtention )) {
            $errors [$fileName] [] = "File format $fileExtention not accepted for $fileName";
            continue;
        }

        if ($fileSize > 2097152) {
            $errors [$fileName] [] = 'reached maxsize of 2MB per file in picture $variable_that_count';
continue ;
        }

        if (count ( $errors ) == 0) {
            $path = "temp";
            $prifix = basename ( $fileName, "." . $fileExtention );

            var_dump ( $prifix );

            $uploadfile = $path . "/" . $fileName;
            $x = 0;
            while ( file_exists ( $uploadfile ) ) {
                $x ++;
                $uploadfile = "{$path}/{$prifix}-{$x}.{$fileExtention}";
            }

            if (move_uploaded_file ( $fileTemp, $uploadfile )) {
                $fileName = basename ( $uploadfile );
                $output [$fileName] = "OK";
            } else {
                $output [$fileName] = "ERORR";
                $errors [$fileName] [] = "Can Move uploaded file to destination";
            }
        }
    }
}

var_dump ( $errors );
var_dump ( $output );

Contoh Keluaran

string '79534296' (length=8)
string '89773706' (length=8)
array
  'download (1)' => 
    array
      0 => string 'File format  not accepted for download (1)' (length=42)
  'brief.docx' => 
    array
      0 => string 'File format docx not accepted for brief.docx' (length=44)
  '' => 
    array
      0 => string 'File format  not accepted for ' (length=30)
array
  '79534296-2.jpg' => string 'OK' (length=2)
  '89773706-2.jpg' => string 'OK' (length=2)

Edit 1

jika semua file harus valid ada 2 cara untuk mencapainya

A. Mulai Dengan memvalidasi semua file terlebih dahulu;

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
    if(! in_array (pathinfo ($_FILES ['image'] ['name'] [$key], PATHINFO_EXTENSION ), $allowedExtention ))
    {
        die("Die! Die! Die") ;
    }
}

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

B. Hapus semua file jika terdeteksi kesalahan

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

// Remove All Files
if(count($errors) > 0)
{
    foreach ($output as $key => $value)
    {
        @unlink($path . "/" . $key);
    }

    die("Die! die! die!") ;
}

Semoga membantu




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySql Amazon RDS:'Fitur Innodb dinonaktifkan' kesalahan dari aplikasi

  2. Bagaimana cara membuat beberapa kueri kolom WHERE IN di pembuat kueri Doctrine?

  3. Mysql:Hitung catatan (termasuk nol) per bulan

  4. SQLAlchemy:Buat kueri penghapusan menggunakan self-join di MySQL

  5. Subquery berkorelasi MySQL dalam sintaks GABUNG