Pages

Wednesday, December 5, 2012

Home >> PHP >> How to upload images and store into php mysql database



There are two ways in php so that we can upload images and call him back. The first way to save the object image into a database and the second way is to store objects in a folder and store the image file name in the mysql database.

Each has its flaws and kelebihanya include:

The first way to save the image object in the database:
Pros: more practical because the store directly into the database
Disadvantages:

Slower database performance and of course the greater the size of the database
It's hard to picture a physical backup as images are saved in the database
The second way to store objects in a folder and store the image file name in mysql:
Pros: file reading process faster because the folders that are stored on the server
Disadvantages: rather complicated process because they have to take the address and name of the first file in the database and then access the folder

I am personally happy with the way the two because if the automatic database backups are smaller and if you want to back up enough to take a picture file folders only. Therefore, I will discuss the second way.

The first step we have prepared the database used to be like the following example:


CREATE TABLE `tb_gambar` (`id` INT NOT  NULL  AUTO_INCREMENT  PRIMARY  KEY ,
 `judul_gambar` VARCHAR( 30  )  NOT  NULL ,
 `nama_file` VARCHAR( 50  )  NOT  NULL
) ENGINE = MYISAM ; 



Please run the above query in the database that you have created.

Then we make uploadnya form, can be stored in the send-gambar.html

<h2>Contoh Upload Gambar</h2>
<form action="upload.php" method="post" enctype="multipart/form-data" name="FUpload" id="FUpload">
  <p>Judul Gambar :
    <input name="judul_gambar" type="text" id="judul_gambar" size="30" maxlength="30" />
</p>
  <p>File Gambar :
    <input name="nama_file" type="file" id="nama_file" size="30" />
</p> 
  <p><input type="submit" name="btnSimpan" id="btnSimpan" value="Simpan" /></p>:


Do not forget that your form has been created using enctype = "multipart / form-data". You can read my article on How to Make a HTML form.

After that we prepare again a PHP file with the name upload.php with code like the following:


<?php
$namafolder="gambar/"; //tempat menyimpan file
$con=mysql_connect("localhost","namauser","pass") or die("Gagal");
mysql_select_db("db_latihan")  or die("Gagal");
if (!empty($_FILES["nama_file"]["tmp_name"]))
{
    $jenis_gambar=$_FILES['nama_file']['type'];
    $judul_gambar=$_POST['judul_gambar'];
    if($jenis_gambar=="image/jpeg" || $jenis_gambar=="image/jpg" || $jenis_gambar=="image/gif" || $jenis_gambar=="image/x-png")
    {           
        $gambar = $namafolder . basename($_FILES['nama_file']['name']);       
        if (move_uploaded_file($_FILES['nama_file']['tmp_name'], $gambar)) {
            echo "Gambar berhasil dikirim ".$gambar;
            $sql="insert into table (judul_gambar,nama_file) values ('$judul_gambar','$gambar')";
            $res=mysql_query($sql) or die (mysql_error());
        } else {
           echo "Gambar gagal dikirim";
        }
   } else {
        echo "Jenis gambar yang anda kirim salah. Harus .jpg .gif .png";
   }
} else {
    echo "Anda belum memilih gambar";
}
?>




Note:

Folder image is created a folder with the file send-gambar.html, upload.php
Upload above just save the file types jpg, gif, and png
Stored in mysql only title picture, the name of the folder and file names of the images
Example of a full script can be downloaded here

If you encounter an error, perhaps my article about the problem about upload file with php can help you.

How to display images that are stored in a mysql database? Please read the article How to display images from mysql database with php

If you want to see examples / guide / tutorial to make a complete application with php can read my article on Creating Student Data Applications with PHP Part 1, Part 2, Part 3, Part 4, Part 5 and Part 6

Hopefully useful. If you have questions or corrections, please do not hesitate to fill in comments. And if this is useful for you please shared to your teman2.

















0 comments:

Post a Comment