Home > PHP > Filterisasi Ekstensi File pada Form Upload

Filterisasi Ekstensi File pada Form Upload

Masih berkenaan dengan Form Upload dengan PHP sebelumnya, kali ini tambahannya hanya pada pembatasan atau filterisasi ekstensi file atau jenis file yang bisa diupload.

Andaikan kita mempunyai sebuah form upload foto, apabila tidak dibatasi, maka akan menjadi senjata bagi para peretas untuk menyisipkan PHP shell atau lainnya. Berbahaya kan tuh?

Yaudah, langsung aja yuk kita cegah, lebih tepatnya mengoreksi koding Form Upload sebelumnya.

<?
error_reporting(0);
?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" name="fupload" id="fupload" />
<input type="submit" name="submit" id="button" value="Submit" />

<?
if($_POST['submit']){
    $lokasi=$_FILES['fupload']['tmp_name'];
    $nama=$_FILES['fupload']['name'];
	$type=$_FILES['fupload']['type'];
    // images adalah folder penyimpanan, $nama sbg nama gambar
    $direktori="images/$nama";

	if($type != "image/gif" || $type != "image/jpeg" || $type != "image/jpg" || $type != "image/png"){
		echo "Upload gagal karena type file: $type";
	}else{
		if(move_uploaded_file($lokasi,"$direktori")){
			echo "Gambar berhasil diupload";
		}else{
			echo "Gambar gagal diupload";
		}
	}
}
?>

Parameter untuk mengetahui type file, terdapat pada kode $type=$_FILES[‘fupload’][‘type’].

Sedangkan parameter pembatasnya, ada pada:

if($type != "image/gif" || $type != "image/jpeg" || $type != "image/jpg" || $type != "image/png"){
		echo "Upload gagal karena type file: $type";
	}

Jadi, selain file format yang tertera diatas tidak akan bisa diupload.

Sekian deh tutorial pendeknya mengenai pembatasan tipe file pada Form Upload. Ada pertanyaan?

Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: