Tuesday, June 2, 2015

PHP: Image Upload



Folder Structure
upload/
connect.php
index.php

tb_image.sql
CREATE TABLE IF NOT EXISTS `tb_image` (
     `ID_image` int(11) NOT NULL AUTO_INCREMENT,
     `file_name` varchar(100) NOT NULL,
     `file_size` varchar(100) NOT NULL,
     `url_path` varchar(100) NOT NULL,
     PRIMARY KEY (`ID_image`)
);

connect.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_php_series';

$db_conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db, $db_conn);

index.php
<?php
/* insert image form */
include('connect.php');

if (isset($_POST['upload'])) {
     $file_name = $_FILES['file']['name'];
     $file_size = $_FILES['file']['size'];
     $upload_path = 'upload/';
     $final_path = $upload_path.$file_name;

     if (move_uploaded_file($_FILES['file']['tmp_name'], $final_path)) {
          mysql_query("INSERT INTO `tb_image`(`file_name`, `file_size`, `url_path`) VALUES ('".$file_name."','".$file_size."','".$final_path."')");
          header("location:index.php");
     } else {
          echo 'general_system_error';
     }
}
?>

<form method="post" action="" enctype="multipart/form-data">
     <input type="file" name="file" />
     <input type="submit" name="upload" value="Upload" />
</form>
/* List view image after insert */
<table width="100%">
     <tr>
          <th></th>
          <th align="left">File Name</th>
          <th align="left">File Size</th>
          <th align="left">Full Path</th>
     </tr>
     <?php
     $query = mysql_query("SELECT * FROM `tb_image` order by ID_image desc");
     while ($data = mysql_fetch_array($query)) {
          echo '<tr>';
          echo '<td><img src="'.$data['url_path'].'" width="50" height="50" /></td>';
          echo '<td>'.$data['file_name'].'</td>';
          echo '<td>'.$data['file_size'].'</td>';
          echo '<td>'.$data['url_path'].'</td>';
          echo '</tr>';
     }
     ?>
</table>

Categories: ,

0 comments:

Post a Comment

luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com

Copyright © Click | Powered by Blogger