mysql - How to make the code continue the process if the file upload section is empty PHP MYSQLi -
i have problem 1 part of upload section, how make code continue process if upload section empty? make code, everytime skip file button, show echo 'error: ekstensi file tidak di izinkan!'
here code:
insert_form
<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>
insert-proses
<?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 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>'; } ?>
any thankful. thanks
jump on complete upload part if there no file uploaded, or better, run upload part in script if file uploaded:
if(isset($_files['file']) && is_array($_files['file'])) { // code upload , check file }
Comments
Post a Comment