programming4us
programming4us
WEBSITE

PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

Inserting the Image into MySQL

Create a new file named insert.php. If you change the name of this file you will also need to change the action="" part in your HTML file above. Open the file in your favorite text editor or PHP IDE.

Write or copy/paste this code into your file:




$username = "YourUserName";

$password = "YourPassword";

$host = "localhost";

$database = "binary";


$link = mysql_connect($host, $username, $password);

if (!$link) {

die('Could not connect: ' . mysql_error());

}



mysql_select_db ($database);

The above code sets your connection variables, creates a connection to MySQL and selects your database (binary if you followed my instructions above). Next we need to read the form data and insert it into the database.


if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {



$tmpName = $_FILES['image']['tmp_name'];


$fp = fopen($tmpName, 'r');

$data = fread($fp, filesize($tmpName));

$data = addslashes($data);

fclose($fp);



$query = "INSERT INTO tbl_images ";

$query .= "(image) VALUES ('$data')";

$results = mysql_query($query, $link);



print "Thank you, your file has been uploaded.";


}

else {

print "No image selected/uploaded";

}



mysql_close($link);

?>

The PHP code above takes the user selected image, reads it with fopen and stores the data in a variable named $data. The binary data is then inserted into our database for retrieval at a later time.

Save the file above and access add.html via your browser. Select an image file and upload it.
Other  
  •  C# Tutorial: Reading and Writing XML Files (part 2) - Reading XML Files
  •  C# Tutorial: Reading and Writing XML Files (part 1) - Writing XML Files
  •  C# 2010 and the .NET 4 Platform : Understanding the ASP.NET Profile API (part 4) - Grouping Profile Data and Persisting Custom Objects
  •  C# 2010 and the .NET 4 Platform : Understanding the ASP.NET Profile API (part 3) - Accessing Profile Data Programmatically
  •  C# 2010 and the .NET 4 Platform : Understanding the ASP.NET Profile API (part 2) - Defining a User Profile Within Web.config
  •  C# 2010 and the .NET 4 Platform : Understanding the ASP.NET Profile API (part 1) - The ASPNETDB.mdf Database
  •  C# 2010 and the .NET 4 Platform : The Role of the sessionState Element - Storing Session Data in the ASP.NET Session State Server, Storing Session Data in a Dedicated Database
  •  C# 2010 and the .NET 4 Platform : ASP.NET State Management Techniques - Understanding Cookies
  •  Sharepoint 2013 : Content Model and Managed Metadata - Publishing, Un-publishing, and Republishing
  •  Sharepoint 2013 : Content Model and Managed Metadata - Content Type Hubs
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
    programming4us
     
     
    programming4us