Junk | Iklan



Monday, February 22, 2010

Upload picture to your website (PHP)

Sebelum ni aku ada post cara nak create form login dengan validation(refer this post)..sekarang aku nak tunjukkan cara senang nak upload gambar ke website korang...

Untuk upload gambar, ada banyak cara seperti gunakan directory folder, gune database entry, thumbnail & textpad files...tapi yang ni aku nak bagitau dulu cara upload guna directory folder. Definisi nye macam ni, anda upload gambar dan gambar tu akan dicopy masuk ke dalam folder pilihan anda contoh : folder upload.

Aku bukan tutor yg ad kelayakan yerrr...aku cume menulis atas pengalaman sbg student..so kalau ade pape salah tlg tunjukkan kesalahan nye ye...:)

Sekarang aku 'assume'anda dah open PHP framework (Dreamweaver i prefer). FIrst thing utk upload perlu lah ade form, dan validator. Dan file utk simpan due2 component tu. Tapi kali ni aku nk gunekan satu file je utk simpan due2 component tersebut. open new file then save as "add_image.php".

Then untuk masukkan form gunekan coding di bawah ni :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="100%" border="0">
<tr>
<td align="center">PHP Code Here</td>
</tr>
<tr>
<td height="257" align="center" valign="top"><form enctype="multipart/form-data" id="form1" name="form1" method="post" action="add_image.php.php">
<table width="41%" border="0">
<tr>
<td height="21" colspan="4" align="center"></td>
</tr>
<tr>
<td width="30%" height="21">Image <input name="MAX_FILE_SIZE" type="hidden" value="1048576" /></td>
<td height="21" colspan="3">:
<input name="image" type="file" /></td>
</tr>
<tr>
<td colspan="4" align="left">&nbsp;</td>
</tr>
<tr>
<td height="21">&nbsp;</td>
<td width="30%" height="21" align="right"><label>
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
</label></td>
<td width="30%"><label></label></td>
<td width="30%">&nbsp;</td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>


dah?save dulu then preview...kalau ok kite proceed next step..

Then, bile form dh ade so mcm mane lak nak insert gambar ke dalam website ye x?Sekarang lihat dengan teliti code kat bawah ni....

Di code form tadi, cari (ctrl+F) "PHP Code Here" pastu replace ayat tu dengan code kat bawah ni :

<?

if(isset($_POST['submitted'])){
if (isset($_FILES['image']))
{
$temp = 'images/' . ($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $temp))
{
$dir='images/';
$image = $dir.$_FILES['image']['name'];
}
}//end

$query= "INSERT INTO Image (img, date)
VALUE('$image', CURDATE())";
$result = mysql_query($query) or die ('error'.mysql_error());
if ($query)
{
echo "<p class='style12'>";
echo "$image has been added";
echo "</p>";
}
else
die ('record error' .mysql_error());
}
?>

*tulisan warna biru adalah folder directory yg anda akan masukkan gamba...boleh diubah mengikut kesesuaian anda.
Thats it...siap file utk upload gamba...PAstikan anda ade database utk image....dan jugak forlder images kt root (klu xubah la)..

create table 'Image' dgn entry "image" dan "date" . . .

then this file can be uploaded to your web directory n can be used....:)

No comments:

Post a Comment