Berfungsi untuk menampilkan form upload
HTML (index.html)
<html>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Filename:
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
2. Membuat uploader.php
Berfungsi untuk mengupload file gambar ke dalam folder upload/
PHP (uploader.php)
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: <textarea>http://www.persegres.com/upload/". $_FILES["file"]["name"]."</textarea>";
}
}
}
else
{
echo "Invalid file";
}
?>
3.Membuat galeri.php
Menampilkan semua gambar dalam folder upload/
PHP (galeri.php)
<?php
$folder = "upload/";
$handle = opendir($folder);
echo '<table cellspacing="2" cellpadding="5">';
echo '<tr>';
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
$fileAndExt = explode('.', $file);
if(in_array(end($fileAndExt), $fileGambar)){
echo '<td style="border:1px solid #000000;" align="center">
<img src="'.$folder.''.$file.'" width="40" height="40" /><br />
'.$file.'
<a href="delete.php?file='.$file.'" onClick="location.href("oldwindowURL")">Delete</a> <br />
</td>';
if(($i % 4) == 0){
echo '</tr><tr>';
}
$i++;
}
}
echo '</tr>';
echo '</table>';
?>
4. Membuat delete.php
Menghapus file gambar yang di pilih.
PHP (delete.php)
<?php
$file=$_GET['file'];
$fileName = 'upload/'.$file.'';
$delete=unlink($fileName);
if($delete){
header ("location:galeri.php");
}
else{
echo 'gagal';}
?>
Tidak ada komentar:
Posting Komentar
Leave a message