php - 如果文件上传部分为空,如何使代码继续该过程 PHP MYSQLi

标签 php mysql

我的上传部分的 1 部分有问题,如果上传部分为空,如何使代码继续该过程?我已经编写了代码,但每次我跳过文件按钮时,它都会显示 echo '错误:Ekstensi file tidak di izinkan!'

这是我的代码:

插入表单

<form name="pegawai" action="insert-proses.php" method="post" enctype="multipart/form-data">
 <table width="700px" align="left" cellpadding="2" cellspacing="0">
 <tr>
  <td><b>NOKOM</b></td>
  <td><b>:</b></td>
  <td><input type="text" name="nokom" size="40" required /></td>
</tr>
<tr>
  <td><b>NIP</b></td>
  <td><b>:</b></td>
  <td><input type="text" name="nip" size="40" required /></td>
</tr>
<tr>
  <td><b>Nama</b></td>
  <td><b>:</b></td>
  <td><input type="text" name="nama" size="40" required /></td>
</tr>
<tr>
  <td><b>Foto</b></td>
  <td><b>:</b></td>
  <td><input name="file" type="file" id="file" /></td>
</tr>
<tr>
  <td></td>
  <td></td>
  <td><hr/><input type="submit" name="upload" value="Upload" /></td>
</tr>
</table>
</form>

插入散文

<?php session_start();
include "config.php";

if(ISSET($_SESSION['superadmin'])) {
    if($_POST['upload']) {
        $nokom      = $_POST['nokom'];      
        $nip        = $_POST['nip'];
        $nama       = $_POST['nama']; 

        // Proses upload file
        $allowed_ext  = array('jpg', 'jpeg', 'png', 'gif');
        $file_name    = $_FILES['file']['name']; // Nama File
        $file_ext     = strtolower(end(explode('.', $file_name))); // Merubah nama file
        $file_size    = $_FILES['file']['size']; // Size File
        $file_tmp     = $_FILES['file']['tmp_name']; // Temp File

        // Proses pengecekan upload file
        if(in_array($file_ext, $allowed_ext) === true) {
            if($file_size < 5220350) // Max upload file 5 MB / 1MB = 1044070 {
                $lokasi = 'files/'.$nama.'.'.$file_ext;
            move_uploaded_file($file_tmp, $lokasi);
            $sql = "INSERT INTO pegawai (nokom,nip,nama,fileext,filegambar)
          VALUES('$nokom','$nip','$nama','$file_ext','$lokasi')";

            if (mysqli_query($conn, $sql)) {
                echo "<script>alert('Insert data berhasil! Klik ok untuk melanjutkan');location.replace('pegawai-list.php')</script>";
            } 
            else {
                echo "Error updating record: " . mysqli_error($conn);
            }
        } else
            {
                echo '<div class="error">ERROR: Besar ukuran file (file size) maksimal 1 Mb!</div>';
            }
    } else
        {
            echo '<div class="error">ERROR: Ekstensi file tidak di izinkan!</div>';
        }
} 
} else
      {
          echo '<script>alert("Anda bukan Superadmin! Silahkan login menjadi Superadminterlebih dahulu");location.replace("index.php")</script>';
      }
?>

任何帮助都会非常感激。谢谢

最佳答案

如果没有上传文件,则跳过完整的上传部分,或者更好的是,仅当上传文件时才在脚本中运行上传部分:

if(isset($_FILES['file']) && is_array($_FILES['file'])) {
    // your code to upload and check the file
}

关于php - 如果文件上传部分为空,如何使代码继续该过程 PHP MYSQLi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30947817/

相关文章:

php - 使用插件 Exec-PHP 在 wordpress 中读取表单

MYSQL 帮助 - 2 个数据库,1 个特色图片

mysql - 如何避免在 Node mysql中插入重复记录?

php - Laravel 图像被发布到数据库为 "/private/var/tmp/"

php - 合并相似的数组,PHP 或 MySQL

php - 在 Google API v2 中,为什么会发生错误 "The project id used to call the Google Play Developer API has not been linked"?

php - 使用 Gmail 新 API 发送带附件的邮件,但不显示在收件人的收件箱中

mysql - 存储过程不工作

mysql - 如何将两个不同的字段值合并到一行中?

mysql - mysql 转储成功后收到电子邮件