Wednesday 9 May 2012

Upload image into folder and save its name into database

This tutorial tell you how to Upload image into folder and save its name into database.
You should follow these two steps:
  1. Creat a database named 'olympic' and a table named 'acc_images'.
    CREATE TABLE IF NOT EXISTS `acc_images` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `acc_id` int(11) unsigned NOT NULL, `image` varchar(255) NOT NULL, `status` varchar(20) NOT NULL, PRIMARY KEY (`id`), );
  2. Copy and past code given below.
    <?php
    // Assigning value about your server to variables for database connection
    $hostname_connect= "localhost";
    $database_connect= "olympic";
    $username_connect= "root";
    $password_connect= "";
    $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
    @mysql_select_db($database_connect) or die (mysql_error());

    if($_POST)
    {
    // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

    if ($_FILES["file"]["error"] > 0)
    {
    // if there is error in file uploading
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

    }
    else
    {
    // check if file already exit in "images" folder.
    if (file_exists("images/" . $_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {  //move_uploaded_file function will upload your image.  if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
    if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))
    {
    // If file has uploaded successfully, store its name in data base
    $query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
    if(mysql_query($query_image))
    {
    echo "Stored in: " . "images/" . $_FILES["file"]["name"];
    }
    else
    {
    echo 'File name not stored in database';
    }
    }
    }


    }
    }
    ?>
    <html>
    <body>
    <form action="" method="post"enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>

17 comments:

  1. hi, once successful upload, how to display it out then?
    thanks for ur help

    ReplyDelete
  2. then select the image name form database and show it in images src after folder or directory name
    e.g.
    <img src="skin1/images/$image_name">

    ReplyDelete
  3. hi..i like your tutorial..
    it's work successful.
    but how to i upload all photos in one process?

    ReplyDelete
  4. hi thank very much .....
    giving a code ..
    for upload and view imgs...
    i search so many web sites for this most of are uncompleted .....
    thanks once again ....

    ReplyDelete
  5. its working good..can any one tell me in clear how to display????

    ReplyDelete
  6. the tutorial is a success man, i would like to have small sizes images when displaying because i want to upload some passport photo and crop them before they are sent to the directory. Regards

    ReplyDelete
  7. I am sending you a link which helps you to upload multiple images at once

    how to upload many images at once in php

    ReplyDelete
  8. thanks for the code...
    Can u plz upload the code,where i can store images and store data in db,with validation....

    I have tried d above code for storing images and other form data,it was working fine until i add validation code

    ReplyDelete
  9. This link will be help full for you for form validation.
    Find if(count($error)==0) and then place above image uploading code between this condition.


    PHP Validation for complete SignUp form

    ReplyDelete
  10. I have tried dis code for uploading videos by making few changes,videos was uploaded successfully..
    But when i merge both d code i.e(image and video uploading). it refresh d same page without storing values in db....

    ReplyDelete
  11. Thanks for your contribution. but after running this script in Mozila, massage is displaied as "No file selected" could you help anybody to solve this problem....

    ReplyDelete
  12. thank u so much. It is really nice

    ReplyDelete
  13. really awesome...

    ReplyDelete