|
|
|
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
| |
|
|
|