Create a table in MySQL with the following :-
CREATE TABLE photos (
name VARCHAR(40);
img LONGBLOB );
The first listing is the HTML for entering information the file to upload.
Once a file has been chosen and a name given 'file.php3' is called.
<?
include ("header.php3"); // A .php3 file containing constant HTML for all docs.
?>
<FORM METHOD=POST ENCTYPE="multipart/form-data"
ACTION="http://localhost/phpSurveys/file.php3">
Upload file<INPUT TYPE=FILE NAME="upload">
Name<INPUT TYPE=TEXT NAME="name">
<INPUT TYPE=SUBMIT VALUE="Upload File">
</FORM>
The next listing shows how to store the data into MySQL.
<?
$fo=fopen($upload,"r");
$fr=fread($fo,1000000);
$img=addslashes($fr);
mysql_connect("localhost","root","");
$insert="INSERT INTO photos (name,img) VALUES ('$name','$img')";
mysql_db_query=("surveys",$insert);
unlink($upload);
?>
The following then calls the image from the database.
Call the following file from another WWW page if you like
using <IMG SRC="getimg.php3" for example.
<?
mysql_connect("localhost", "root", "");
$query="select image from photos where name=('test.jpg')";
$result=mysql_db_query("surveys",$query);
$rows=mysql_num_rows($result);
if($rows<1)
{
print "no image";
}
else
{
$photo=mysql_fetch_object($result);
Header("Content-type:image/jpeg");
$body=$photo->image;
echo $body;
flush();
}
?>
I know there is no error checking in this but this is only intended as an
example to help people stuck in a corner like I was.
Most of the above is thanks to an article written by mailto:jpatrick@hellyeah.com
which I discovered in a posting at http:/www.geocrawler.com
Robert W. Castley
Group Professional Services Consultant, Macro 4
http://www.macro4.com
Macro 4 plc, Fairfield House, Carey Road, Wokingham RG40 2NP
Switchboard: +44 (0)1189 892626 Fax: +44 (0)1189 890414