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

Beberapa Masalah Unggah Gambar

Untuk kesalahan pertama, larik kesalahan berada di luar foreah sehingga Anda tidak menghapus kesalahan untuk setiap file, dan kueri untuk menyisipkan berada di luar if yang memeriksa kesalahan sehingga selalu dieksekusi

Untuk kesalahan kedua yang sama, kueri dijalankan selalu Anda harus memeriksa apakah file diunggah.

<?php

    include_once('connect.php');

    if(isset($_FILES['files'])){
        $filesErrors = 0;
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
            // MOved errors inside the foreach to clear it each loop
            $errors = array();

            // Check file is uploaded
            if ($_FILES['files']['error'][$key] == UPLOAD_ERR_NO_FILE){
                // Continue with the next file
                continue; 
            }

            $file_name = $key.$_FILES['files']['name'][$key];
            $file_size = $_FILES['files']['size'][$key];
            $file_type = $_FILES['files']['type'][$key];

            if($file_type == "image/gif"){
                $sExt = ".gif";
            } elseif($file_type == "image/jpeg" || $file_type == "image/pjpeg"){
                $sExt = ".jpg";
            } elseif($file_type == "image/png" || $file_type == "image/x-png"){
                $sExt = ".png";
            }
            if (!in_array($sExt, array('.gif','.jpg','.png'))) {
                $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
            }

            $file_tmp = $_FILES['files']['tmp_name'][$key];
            $file_type = $_FILES['files']['type'][$key];
            if($file_size > 2097152){
                $errors[]='File size must be less than 2 MB';
            }    


            if(empty($errors) == true){
                $desired_dir = "user_data";
                // Execute query inside the errors check
                $query = "INSERT into offers_pics (`offer_id`,`pic_name`,`pic_type`) VALUES ('$user_id','$file_name','$file_type'); ";
                $result = mysqli_query($link,$query);

                if(is_dir($desired_dir) == false){
                    mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                }
                if(is_dir("$desired_dir/".$file_name) == false){
                    move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                }else{                                  // rename the file if another one exist
                    $new_dir = "$desired_dir/".$file_name.time();
                     rename($file_tmp,$new_dir) ;               
                }
            }else{
                $filesErrors++;
                print_r($errors);
            }
        }

        if ($filesErrors == 0){
            echo 'Success';
        }
    }
?>


<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]"> <br/>
    <input type="file" name="files[]"> <br/>
    <input type="file" name="files[]"> <br/>
    <input type="file" name="files[]"> <br/>
    <input type="file" name="files[]" > <br/><br/>
    <input type="submit"/>
</form>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pengecualian di utas utama org.hibernate.HibernateException:Kesalahan saat mengakses aliran stax

  2. Mengisi DataTable di C# menggunakan MySQL

  3. tampilan daftar pengguna teman tidak menampilkan semua pengguna

  4. Mysql mengubah mesin tabel MyISAM ke InnoDB

  5. Menangani integritas basis data