WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Loading an image to the db
Categories : MySQL, PHP, Databases Click here to Update Your Picture
bastien koert
Date : Jan 26th 2005
Grade : 2 of 5 (graded 5 times)
Viewed : 7514
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Here is a quick n dirty image uploader to a mysql db

Table structure:
CREATE TABLE binary_data (
id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
description CHAR(50),
bin_data LONGBLOB,
filename CHAR(50),
filesize CHAR(50),
filetype CHAR(50)
);



<html>
<head><title>Store binary data into SQL Database</title></head>
<body>

<?php
// code that will be executed if the form has been submitted:


if ((isset($_POST['submit']))&&(isset($_FILES['form_data']))) {
 
upload();
} else {
 
show_form();
}
//end if

function upload() {
//includes
require("conn.php");

   
//define variables
   
$image_file  = '';
   
$image_desc  = '';
   
$type        = '';
   
$size        = 0;
   
$name        = '';
   
   
$image_file = $_FILES['form_data']['tmp_name'];
   
$image_desc = $_POST['form_description'];
   
$type       = $_FILES['form_data']['type'];
   
$size       = $_FILES['form_data']['size'];
   
$name       = $_FILES['form_data']['name'];
   
   
$data = fread(fopen($image_file, "r"), filesize($image_file));
   
$data = addslashes($data);
   
$result=connect("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
   
"VALUES ('$image_desc','$data','$name','$size','$type')");
   
   
$id= mysql_insert_id();
    print
"<p>This file has the following Database ID: <b>$id</b>";
   
   
//show the form again to load the next image
   
show_form();
}

function
show_form()
{
// else show the form to submit new data:
?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description" size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data" size="40">
    <p>
    <input type="submit" name="submit" value="submit">
    <input type="button" name="move" value="Show" onClick="javascript:window.location='show_desc.php';">
    </p>
    </form>

<?php
}
?>
</body>
</html>



This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Alternating background color for HTML table rows
Categories : PHP, Databases, MySQL, HTML and PHP
color codes for positive and negative numbers
Categories : PHP, MySQL, Databases, HTML
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
 matthew waygood wrote : 1263
$data = fread(fopen($image_file, "r"), filesize($image_file)); 
Try using "rb" instead of just "r", as b is binary safe.

I`d use is_uploaded_file() and not just isset($_FILE[``])

Also why put the form in a function, it will always be 
displayed so just put it at the bottom of the page.

<?php
check
//code
?>
<form ....

Since you are posting to the same page, you may also want 
to add an additional piece of information, to prevent 
double posts. Since doing a refresh would re-submit the 
info, allowing people to insert the same image multiple 
times.

Add in `created VARCHAR(100)`, and <input name="date_created" value="<?php echo date("Y-m-d H:i:s").$_SERVER[`REMOTE_ADDR`]; "/>

$sql="
SELECT FROM table WHERE created=`".$_POST[`created`].$_SERVER[`REMOTE_ADDR`]."`";
$result=doQuery($sql);
if(mysql_num_rows($result)>0)
{
  // double submit - report error
}
else
{
  // insert into DB code
}
 
 matthew waygood wrote : 1264
sorry was going with date_created, but changed my mind, so 
input name is wrong. Since 2 people could open the form at 
the same time I added in their IP address aswell.
 
 bastien koert wrote : 1266
Hi Matt,

Thanks for the comments. But that`s why is says `quick and 
dirty`. Wasn`t meant to be all inclusive and totally 
secure and idiot proof...more  of a concept piece
 
 mike ar wrote : 1756
$result = connect("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
    "VALUES ('$image_desc','$data','$name','$size','$type')");
   
Fatal error: Call to undefined function connect() in /home/techker/public_html/Scripts/imageupload.php on line 34

do i have to change something?
 
 bastien koert wrote :1757
connect function is here

http://www.weberdev.com/get_example-3992.html