How to upload images to mySQL within PHP
by Scud Tuesday, August 08, 2006
Rating:
Vote this news:
Aim of this article is to sho you the simples way to uplaod images to mySQL database. First you need to create a custom database table. The name of this table can be named as "images_table". Now here is the SQL code for creating the table.
images_table.sql
CREATE TABLE `images_table` (
`imageID` int(11) NOT NULL auto_increment,
`imageName` varchar(25) NOT NULL default '',
`imageFile` longtext NOT NULL,
PRIMARY KEY (`imageID`)
) AUTO_INCREMENT=1 ;
This is our upload HTML file. There will be no server-side coding on this file :
imageupload.php
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="imageupload.php" method="POST" enctype="multipart/form-data">
Name : <input type="text" name="imageName"> <br/>
Image :<input type="file" name="imageFile">
<input type="submit" value="Upload" name="func">
</form>
</body>
</html>
Now this is the rest of the code. This php file will insert the image to our database.
imageupload.php
<?
/*
This function take the image from form variables
*/
function getImageFile($file){
$takeFile = fopen($file, "r");
$file = fread($takeFile, filesize($file));
fclose($takeFile);
return $file;
}
/*
We learn image type using this function
Because we will let onlt gif, jpg and png images can be uploaded
*/
function getfileType( $name ){
$name = explode(".", $name);
$name = array_reverse($name);
$name = $name[0];
return $name;
}
$allowedImageTypes = array("gif","jpg","png");
if(empty($_FILES['image_file']['tmp_name'])){
echo "File not uploaded";
}
else {
$fileType = $_FILES['image_file']['name'];
if(in_array(getfileType($fileType), $allowedImageTypes)){
$fileContent = getImageFile($_FILES['imgFile']['tmp_name']);
$uploadedImage = chunk_split(base64_encode($fileContent));
$query = "INSERT INTO images_table VALUES('NULL','$imgName','$uploadedImage')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0){
echo "Image has been inserted succesfully";
}
else {
echo "Image can not be inserted check your submission";
}
}
else {
echo "This is not a true image type";
}
}
Happy Coding
images_table.sql
CREATE TABLE `images_table` (
`imageID` int(11) NOT NULL auto_increment,
`imageName` varchar(25) NOT NULL default '',
`imageFile` longtext NOT NULL,
PRIMARY KEY (`imageID`)
) AUTO_INCREMENT=1 ;
This is our upload HTML file. There will be no server-side coding on this file :
imageupload.php
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="imageupload.php" method="POST" enctype="multipart/form-data">
Name : <input type="text" name="imageName"> <br/>
Image :<input type="file" name="imageFile">
<input type="submit" value="Upload" name="func">
</form>
</body>
</html>
Now this is the rest of the code. This php file will insert the image to our database.
imageupload.php
<?
/*
This function take the image from form variables
*/
function getImageFile($file){
$takeFile = fopen($file, "r");
$file = fread($takeFile, filesize($file));
fclose($takeFile);
return $file;
}
/*
We learn image type using this function
Because we will let onlt gif, jpg and png images can be uploaded
*/
function getfileType( $name ){
$name = explode(".", $name);
$name = array_reverse($name);
$name = $name[0];
return $name;
}
$allowedImageTypes = array("gif","jpg","png");
if(empty($_FILES['image_file']['tmp_name'])){
echo "File not uploaded";
}
else {
$fileType = $_FILES['image_file']['name'];
if(in_array(getfileType($fileType), $allowedImageTypes)){
$fileContent = getImageFile($_FILES['imgFile']['tmp_name']);
$uploadedImage = chunk_split(base64_encode($fileContent));
$query = "INSERT INTO images_table VALUES('NULL','$imgName','$uploadedImage')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0){
echo "Image has been inserted succesfully";
}
else {
echo "Image can not be inserted check your submission";
}
}
else {
echo "This is not a true image type";
}
}
Happy Coding
All comments
??
by techker 3/3/2008
it's not working for me at all??file not uploaded or blank ?what version of sql /php is this?



