| PhotoShop Tutorials | | PHP Tutorials | | CSS Tutorials | | PhotoShop Brushes Downloads | | Fonts Downloads | | Pimp MySpace |




First, all mySQL based PHP scripts need
a mySQL table....


CREATE TABLE `images` (
`image_id` int(11) NOT NULL auto_increment,
`image_name` varchar(25) NOT NULL default '',
`image_base64` longtext NOT NULL,
PRIMARY KEY (`image_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


That is the mySQL query that will create the table we will be using throughout the tutorial.

Now first, we need a simple HTML form to collect the image name and the specific file,
Create a file called uploadform.html

<html>


<head>
<title>Uploading files into mySQL databases</title>
</head>

<body>

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="text" name="imgName"> :: Image Name<br>
<input type="file" name="imgFile"> :: Image File<br>
<input type="submit" value="Upload">
</form>

</body>
</html>


All of this should be at least recognized or you know how it works.
Now we have the basics set up, lets learn how to actually upload the image into the mySQL database, lets take a look at some code.

Create a file called upload.php

<?
function filetxt($file){ // Return the contents of the file in text form
$file_o = fopen($file, "r");
$file = fread($file_o, filesize($file));
fclose($file_o);

return $file;
}

function fileExt( $name ){
// This is the function that return the file type (gif, png, jpg, or any file)
$name = explode(".", $name);
$name = array_reverse($name);
$name = $name[0];

return $name;
}

$allowedFiles = array("gif","jpg","png");
if(empty($_FILES['image_file']['tmp_name'])){
echo "File not uploaded";
}
else {
$fileType = $_FILES['image_file']['name'];

if(in_array(fileExt($fileType), $allowedFiles)){
$fileContent = filetxt($_FILES['imgFile']['tmp_name']);

$base64 = chunk_split(base64_encode($fileContent));

$query = "INSERT INTO images VALUES('NULL','$imgName','$base64')";

$result = mysql_query($query);

if(mysql_affected_rows() > 0){

echo "File uploaded";
}
else {
echo "Could not upload file";
}
}
else {
echo "File type not allowed";
}
}

Ill break this down, first. Its checking to see whether or not an image is uploaded, then it goes on to convert the content of the file, into a base64 text, then puts it in
to the mySQL database.

Now we need to create the file that will display the image...

Create a file called imagedisplay.php

<?

$id = $_REQUEST['id'];
$query = "SELECT * FROM images WHERE image_id='$id'";
$result = mysql_query($query);

$imageArray = mysql_fetch_array($result);

$image_base64 = base64_decode($imageArray['image_base64']);

echo $image_base64;

?>


What this does is call to the row with the id defined by the $id variable.
So when you want to display the image...

<img src="imagedisplay.php?id=1">


This will display the image with the id of 1.





| SiteMap | | Proxy SiteMap | | Free Mortgage Calculators | Myspace Falling Object | | Myspace Survey | | Myspace Slideshows | |Toms MySpace Editor |
CopyRight 2005 Radialdesigns.co.uk