Pada Postingan kali ini saya ingin sharing tentang bagaimana cara mengupload dan menyimpan gambar dengan menggunakan Mysql dan PHP.. berikut caranya dibawah
- Buat database dengan nama db_tutorial
- Siapkan tabel dengan nama tb_image, dengan struktur tabel seperti gambar dibawah ini.
3.Done with the database!
Step 2 : Mempersiapkan Folder Kerja / Workspace
- Buat folder dengan nama helloMobile dalam document root anda
- Buat lagi folder dengan nama image didalam folder helloMobile yang telah anda buat sebelumnya. Folder image ini adalah folder yang akan digunakan untuk menyimpan gambar hasil upload.
- Simpan semua file dalam praktikum ini dalam folder helloMobile tersebut.
Step 3 : Membuat Script Koneksi ke DataBase
Copy Kode Script dibawah
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbName = "db_tutorial";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>
Simpan dengan nama connect.php . simpan dalam folder kerja
Step 4 : Membuat Form Upload Image
Copy Script dibawah
<form name="form" method="post" enctype="multipart/form-data" action="proses.php">
Image : <input name="picture" type="file" />
<input type="submit" name="upload" value="Upload" />
</form>
Simpan dengan nama formupload.php. simpan dalam folder kerja
Step 5 : Membuat script pemrosesan dan menampilkan gambar hasil upload
Copy Script dibawah
<?php
include "connect.php";
$fileName = $_FILES['picture']['name']; //get the file name
$fileSize = $_FILES['picture']['size']; //get the size
$fileError = $_FILES['picture']['error']; //get the error when upload
if($fileSize > 0 || $fileError == 0){ //check if the file is corrupt or error
$move = move_uploaded_file($_FILES['picture']['tmp_name'], 'E:/DocumentRootYuni/helloMobile/image/'.$fileName); //save image to the folder
if($move){
echo "<h3>Success! </h3>";
$q = "INSERT into tb_image VALUES('','$fileName','image/$fileName')"; //insert image property to database
$result = mysql_query($q);
$q1 = "SELECT location from tb_image where filename = '$fileName' limit 1 "; //get the image that have been uploaded
$result = mysql_query($q1);
while ($data = mysql_fetch_array($result)) {
$loc = $data['location']; ?>
<br/>
<h2> This is the Image : </h2>
<img src="<?php echo $loc; ?>" /> <!-- show the image using img src -->
<?php
}
} else{
echo "<h3>Failed! </h3>";
}
} else {
echo "Failed to Upload : ".$fileError;
}
?>
Simpan dengan nama prosesupload.php simpan pada folder kerja anda !
tinggal cek hasil :)
sumber : angger-irwan. blogspot. com
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbName = "db_tutorial";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>
Simpan dengan nama connect.php . simpan dalam folder kerja
Step 4 : Membuat Form Upload Image
Copy Script dibawah
<form name="form" method="post" enctype="multipart/form-data" action="proses.php">
Image : <input name="picture" type="file" />
<input type="submit" name="upload" value="Upload" />
</form>
Simpan dengan nama formupload.php. simpan dalam folder kerja
Step 5 : Membuat script pemrosesan dan menampilkan gambar hasil upload
Copy Script dibawah
<?php
include "connect.php";
$fileName = $_FILES['picture']['name']; //get the file name
$fileSize = $_FILES['picture']['size']; //get the size
$fileError = $_FILES['picture']['error']; //get the error when upload
if($fileSize > 0 || $fileError == 0){ //check if the file is corrupt or error
$move = move_uploaded_file($_FILES['picture']['tmp_name'], 'E:/DocumentRootYuni/helloMobile/image/'.$fileName); //save image to the folder
if($move){
echo "<h3>Success! </h3>";
$q = "INSERT into tb_image VALUES('','$fileName','image/$fileName')"; //insert image property to database
$result = mysql_query($q);
$q1 = "SELECT location from tb_image where filename = '$fileName' limit 1 "; //get the image that have been uploaded
$result = mysql_query($q1);
while ($data = mysql_fetch_array($result)) {
$loc = $data['location']; ?>
<br/>
<h2> This is the Image : </h2>
<img src="<?php echo $loc; ?>" /> <!-- show the image using img src -->
<?php
}
} else{
echo "<h3>Failed! </h3>";
}
} else {
echo "Failed to Upload : ".$fileError;
}
?>
Simpan dengan nama prosesupload.php simpan pada folder kerja anda !
tinggal cek hasil :)
sumber : angger-irwan. blogspot. com
No comments:
Post a Comment