Upload Photo Dengan PHP
Berikut
ini adalah contoh script sederhana untuk proses upload photo yang semoga bisa
memunculkan ide-ide baru serta kedepan dapat dikembangkan lagi kebentuk yang
lebih kompleks sesuai dengan keperluan anda.
Langkah
pertama :
Simpan
source code berikut dengan nama upload.php atau sesuai keinginan
anda.
1)
<html>
2)
<head>
3)
<title>Unggah Photo Anda</title>
4)
<style type=”text/css”>
5) <!–
6) .style1
{
7)
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:
12px;
9) color:
#008000;
10) }
11)
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;
color: #008000; font-weight: bold; }
12) –>
13)
</style>
14)
<body>
15)
16)
<?php
17)
if($_POST["button"] == “Upload” {
18)
$tipe_yg_blh = array
19)
(“image/bmp”,”image/jpeg”,”image/pjpeg”,”image/gif”,”image/x-
20) png”;
21)
$nama_file = $_FILES["gambar"]["name"];
22)
$ukuran_file = $_FILES["gambar"]["size"];
23)
$tipe_file = $_FILES["gambar"]["type"];
24)
$nama_baru = $ukuran_file.$nama_file;
25)
26)
if($ukuran_file != 0){
27) if
(in_array ($tipe_file,$tipe_yg_blh)){
28)
move_uploaded_file($_FILES["gambar"]["tmp_name"],”images/$nama_
29) baru”;
30)
$gambar=”<div
31)
align=’center’><img src=’images/$nama_baru’ width’250′
32)
height=’200′></div>”;
33) ?>
34)
<form method=”POST”
35)
action=”hapus.php” enctype=”multipart/form-data”>
36)
<table align=”center”>
37)
<tr>
38) <td
height=”30″
39)
colspan=”3″ align=”center”><?php echo $gambar; ?></td>
40)
</tr>
41)
<tr>
42) <td
height=”30″
43) colspan=”3″
align=”center”><?php echo $nama_file; ?></td>
44)
</tr>
45)
<tr>
46) <td
height=”30″
47)
colspan=”3″ align=”center”>
48)
<input type=”submit” name=”button”
49)
value=”Hapus” onClick=”return confirm(‘Apakah anda yakin akan
50)
menghapus photo ini ?’”>
51)
</td>
52)
</tr>
53)
</table>
54)
<input type=”hidden” name=”nama_file”
55)
value=”<?php echo $nama_baru; ?>”>
56)
57)
</form>
58)
<?php
59) }else{
60) echo
“<script>alert(‘Upzzz, tipe
61) file
ini gax boleh …^_^…’;window.history.go(1);</script>”;
62)
63)
exit();
64) }
65) }else{
66) echo
“<script>alert(‘Upzzz, pilih gambar
67) dulu
dunx …^_^…’;window.history.go(-1);</script>”;
68)
exit();
69) }
70) }else{
71) ?>
72)
73)
<form method=”POST” action=”" enctype=”multipart/form-data”>
74) <table
border=”0″ align=”center”>
75)
<tr>
76) <td
height=”50″ colspan=”3″><div align=”left”><span
class=”style3″>Silahkan pilih photo anda</span>
</div></td>
77)
</tr>
78)
<tr>
79)
<td><input name=”gambar” type=”file” class=”style1″ id=”gambar”
size=”25″></td>
80)
</tr>
81)
<tr>
82) <td
height=”30″ colspan=”3″ align=”center”><input type=”submit” name=”button”
class=”style1″ value=”Upload”>
83)
<label>
84)
<input name=”button2″ type=”reset” class=”style1″ id=”button”
value=”Reset”>
85)
</label></td>
86) </tr>
87)
</table>
88)
</form>
89)
90)
<?php
91) }
92) ?>
93)
94)
</body>
95)
</html>
Langkah
kedua:
Simpan
source code berikut dengan nama hapus.php.
1)
<?php
2)
unlink(“images/$nama_file”;
3)
echo”<script>window.location.href=’upload.php’
4)
;</script>”;
5) ?>
Simpan dua script diatas di folder webserver
yang telah anda setting sebelumnya dan jalankan file upload.php melalui browser and.
Komentar
Posting Komentar