How to store file name in database, with other info while uploading image to server using PHP? How to store file name in database, with other info while uploading image to server using PHP? database database

How to store file name in database, with other info while uploading image to server using PHP?


Here is the answer for those of you looking like I did all over the web trying to find out how to do this task. Uploading a photo to a server with the file name stored in a mysql database and other form data you want in your Database. Please let me know if it helped.

Firstly the form you need:

    <form method="post" action="addMember.php" enctype="multipart/form-data">    <p>              Please Enter the Band Members Name.            </p>            <p>              Band Member or Affiliates Name:            </p>            <input type="text" name="nameMember"/>            <p>              Please Enter the Band Members Position. Example:Drums.            </p>            <p>              Band Position:            </p>            <input type="text" name="bandMember"/>            <p>              Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.            </p>            <p>              Photo:            </p>            <input type="hidden" name="size" value="350000">            <input type="file" name="photo">             <p>              Please Enter any other information about the band member here.            </p>            <p>              Other Member Information:            </p><textarea rows="10" cols="35" name="aboutMember"></textarea>            <p>              Please Enter any other Bands the Member has been in.            </p>            <p>              Other Bands:            </p>            <input type="text" name="otherBands" size=30 />            <br/>            <br/>            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>          </form>

Then this code processes you data from the form:

   <?php// This is the directory where images will be saved$target = "your directory";$target = $target . basename( $_FILES['photo']['name']);// This gets all the other information from the form$name=$_POST['nameMember'];$bandMember=$_POST['bandMember'];$pic=($_FILES['photo']['name']);$about=$_POST['aboutMember'];$bands=$_POST['otherBands'];// Connects to your Databasemysqli_connect("yourhost", "username", "password") or die(mysqli_error()) ;mysqli_select_db("dbName") or die(mysqli_error()) ;// Writes the information to the databasemysqli_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;// Writes the photo to the serverif(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){// Tells you if its all okecho "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";}else {// Gives and error if its notecho "Sorry, there was a problem uploading your file.";}?> 

Code edited from www.about.com


You have an ID for each photo so my suggestion is you rename the photo. For example you rename it by the date

<?php $date = getdate(); $name .= $date[hours]; $name .= $date[minutes]; $name .= $date[seconds]; $name .= $date[year]; $name .= $date[mon]; $name .= $date[mday];?>

note: don't forget the file extension of your file or you can generate random string for the photo, but I would not recommend that. I would also recommend you to check the file extension before you upload it to your directory.

<?php if ((($_FILES["photo"]["type"] == "image/jpeg")            || ($_FILES["photo"]["type"] == "image/pjpg"))            && ($_FILES["photo"]["size"] < 100000000))            {                move_uploaded_file($_FILES["photo"]["tmp_name"], $target.$name);                if(mysql_query("your query"))                {                    //success handling                }                else                 {                    //failed handling                }            }            else            {                //error handling            }?>

Hope this might help.


<form method="post" action="addMember.php" enctype="multipart/form-data">    <p>              Please Enter the Band Members Name.            </p>            <p>              Band Member or Affiliates Name:            </p>            <input type="text" name="nameMember"/>            <p>              Please Enter the Band Members Position. Example:Drums.            </p>            <p>              Band Position:            </p>            <input type="text" name="bandMember"/>            <p>              Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.            </p>            <p>              Photo:            </p>            <input type="hidden" name="size" value="350000">            <input type="file" name="photo">             <p>              Please Enter any other information about the band member here.            </p>            <p>              Other Member Information:            </p><textarea rows="10" cols="35" name="aboutMember"></textarea>            <p>              Please Enter any other Bands the Member has been in.            </p>            <p>              Other Bands:            </p>            <input type="text" name="otherBands" size=30 />            <br/>            <br/>            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>          </form>

save it as addMember.php

<?php//This is the directory where images will be saved$target = "your directory";$target = $target . basename( $_FILES['photo']['name']);//This gets all the other information from the form$name=$_POST['nameMember'];$bandMember=$_POST['bandMember'];$pic=($_FILES['photo']['name']);$about=$_POST['aboutMember'];$bands=$_POST['otherBands'];// Connects to your Databasemysql_connect("yourhost", "username", "password") or die(mysql_error()) ;mysql_select_db("dbName") or die(mysql_error()) ;//Writes the information to the databasemysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;//Writes the photo to the serverif(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){//Tells you if its all okecho "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";}else {//Gives and error if its notecho "Sorry, there was a problem uploading your file.";}?>

in the above code one little bug ,i fixed that bug.