Image Upload With PHP

So, we are going to try to upload image through PHP, and then save the uploaded image into a folder in the computer, and the save the folder's location in the SQL database.
It's quite easy. Please prepare your SQL database anda also any file editor that you always use.
All set? These are the steps:
1. Create New Database
Create new database "upload2" or any other names that you like. after that, create new table "store" or any other names that you like. And please make sure that the table "store" has the following structures:
Table "store":
CREATE TABLE store (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(500) NOT NULL,
location VARCHAR(500) NOT NULL,
PRIMARY KEY (id)
);
2. PHP Files
You will create 4 PHP files:
Here's how it works. The File "index.php" is the main file where you will choose which image that you want to upload. After that, once you've clicked on the submit button, the file "upload.php" is responsible when the uploading process starts. The File "view.php" will be used to view the images that have been uploaded. and the file "edit.php" will be used to replace a current particular image into a new image.
index.php:
<html>

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile" required accept="image/jpeg"><br/>
    <input type="submit" name="submit" value="Upload">
</form>

</html>

upload.php:
<?php

mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());

if($_POST['submit'])
{

    $name=$_FILES['myfile']['name'];
    $type=$_FILES['myfile']['type'];
    $size=$_FILES['myfile']['size'];
    $temp=$_FILES['myfile']['tmp_name'];

    if(!$name)
        {
            die("Error!");
        }
    else
        {
            if($type=="image/jpeg" && $size<102400) //file size is in Bytes
                {
                $timestamp=strtotime("now");
                $file_name="img".$timestamp."-".$name."";
                $location="img/$file_name";
                move_uploaded_file($temp,$location);
                $query=mysql_query("INSERT INTO store (name,location) VALUES ('$file_name','$location')");
                echo "OK!";
                }
            else
                {
                echo "The format of the file is not allowed!";
                }
        }

}

?>

view.php:
<?php
mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());

$query = mysql_query("SELECT * FROM store");
  while($row=mysql_fetch_array($query))
  {
    echo $row['name'];
    $location=$row['location'];
    echo "<img src='$location' width='100' height='100'>";
    echo "<br/>";
  }
?>

edit.php:
<html>

<?php
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());
?>

<form action="edit.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile" required accept="image/jpeg"><br/>
    <input type="submit" name="submit" value="Upload">
</form>

</html>

<?php
if($_POST['submit'])
{
    $name=$_FILES['myfile']['name'];
    $type=$_FILES['myfile']['type'];
    $size=$_FILES['myfile']['size'];
    $temp=$_FILES['myfile']['tmp_name'];
    
/* WARNING
For the sake of this tutorial, you have to change the id below to the image's id that you want to edit.
Untuk keperluan tutorial ini, anda harus menentukan id gambar mana yang mau anda edit */
    $id=13;

    $query=mysql_query("SELECT * FROM store WHERE id='$id'");
    $row=mysql_fetch_array($query);
    $old_location=$row['location'];
    $delete_file=unlink($old_location);
    
    if($delete_file)
    {
        if(!$name)
        {
            die("Error!");
        }
        else
        {
            if($type=="image/jpeg" && $size<102400) //file size is in Bytes
                {
                $timestamp=strtotime("now");
                $file_name="img".$timestamp."-".$name."";
                $location="img/$file_name";
                move_uploaded_file($temp,$location);
                $query=mysql_query("UPDATE store SET name='$file_name', location='$location' WHERE id='$id'");
                echo "OK!";
                }
                else
                {
                echo "The format of the file is not allowed!";
                }
        }
    }
    else
    {
        echo "Ooops...!";
    }
}
?>

Once you've done all of the files, use the file "index.php" to start uploading images. after that, use the file "view.php" to view the uploaded images. and lastly, please use the file "edit.php" to replace a current particular image with another image.
Good luck...!

Labels: , , , , , , ,