<form action="upload-hapus.php?act=upload" method="post" enctype="multipart/form-data">
<p><input name="fgambar" type="file" id="fgambar" /></p>
<p>Tipe file yang diperbolehkan jpg, jpeg, pjpeg, png atau gif.</p>
<p>Deskripsi:</p>
<p><textarea name="deskripsi" cols="38" rows="5" id="deskripsi">
</textarea></p>
<input type="hidden" name="direktori" id="direktori" value="images" />
<p><input name="upload" type="submit" value="Upload" id="upload" /></p>
</form>
Untuk menampilkan gambar yang telah di upload:
<?php$SQL ="SELECT * FROM file_gambar";
$qryGambar =@mysql_query($SQL);
$no=0;
while($row =@mysql_fetch_array($qryGambar)):
$no++;
$conf ="Anda yakin akan menghapus gambar ".$row['file_name'];
echo '<p>No : '.$no.'</p>';
echo '<p>Nama : '.$row['file_name'].' | <a href="upload-hapus.php?act=hapus&id='.$row['file_id'].'" onclick="return confirm(\'Anda yakin akan menghapus gambar '.$row['file_name'].'\');">Hapus</a></p>';
echo '<p>Ukuran : '.$row['ukuran'].' bytes</p>';
echo '<p><img src="'.$row['direktori'].'/'.$row['file_name'].'" /></p>';
echo '<p>Deskripsi : '.$row['deskripsi'].'</p><hr />';
endwhile;
if($no==0){
echo "<p>Gambar belum tersedia.</p>";
}
?>Skrip untuk meng-upload gambar:
<?phpinclude "koneksi.php";
if($_GET['act']=='upload'):
$tipe =$_FILES['fgambar']['type'];
if( $tipe != "image/jpg" AND
$tipe != "image/jpeg" AND
$tipe != "image/pjpeg" AND
$tipe != "image/png" AND
$tipe != "image/gif"){
echo '<p><b>Upload Gagal</b></p>';
echo '<p>Tipe file yang diperbolehkan jpg, jpeg, pjpeg, png atau gif.</p>';
echo '<p><a href="index.php">ULANGI</p></p>';
}else{
$file_name =$_FILES['fgambar']['name'];
$ukuran =$_FILES['fgambar']['size'];
$direktori =$_POST['direktori'];
$deskripsi =$_POST['deskripsi'];
$SQL ="SELECT file_name FROM file_gambar WHERE file_name='$file_name'";
$qGambar =@mysql_query($SQL);
if(count(@mysql_fetch_array($qGambar)) > 1){
echo '<p><b>Upload Gagal</b></p>';
echo '<p>Gambar telah tersedia</p>';
echo '<p><a href="index.php">KEMBALI</p></p>';
}else{
move_uploaded_file($_FILES['fgambar']['tmp_name'],$direktori.'/'.$file_name);
@mysql_query("INSERT INTO file_gambar"
SET file_name ='$file_name',
ukuran ='$ukuran',
direktori ='$direktori',
deskripsi ='$deskripsi'");"
header('location:index.php');
}
}
endif;
?>Skrip untuk meng-hapus gambar:
<?phpif($_GET['act']=='hapus'):
$id =$_GET['id'];
$SQL ="SELECT * FROM file_gambar WHERE file_id='$id'";
$qGambar =@mysql_query($SQL);
$row =@mysql_fetch_array($qGambar);
if(file_exists($row['direktori'].'/'.$row['file_name']))
unlink($row['direktori'].'/'.$row['file_name']);
@mysql_query("DELETE FROM file_gambar WHERE file_id='$id'");
header('location:index.php');
endif;
?>Belajar pintar php