Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Inserting an image as a blob in PHP

Posted by andrewtayloruk (andrewtayloruk), 23 March 2007
Hi Graham and everyone else on the forums. I was wondering if somebody on here could help...

I've made a form upload script that inserts the data from the forms into an  sql database. The thing i'm stuck on is getting the image from the final input box and putting it into the database as a blob.

*** Code below ***

<?php
if (isset($_POST['submitted'])) {

 $name = trim(strip_tags($_POST['name']));

 $address = trim(strip_tags($_POST['address']));

 $postcode = trim(strip_tags($_POST['postcode']));

 $telephone = trim(strip_tags($_POST['telephone']));

 $email = trim(strip_tags($_POST['email']));

 $picture = trim(strip_tags($_POST['picture']));

 $dbid = mysql_connect ('localhost', 'root');

 mysql_select_db('addresses',$dbid)

   or die ("Cannot find database");

 $query = "INSERT INTO `book` (`aid`, `name`, `address`, `postcode`, `telephone`, `email`, `picture`) VALUES ('', '$name', '$address', '$postcode', $telephone, '$email' , '$picture')";

 $result = mysql_query($query,$dbid)

   or die("INSERT error:".mysql_error());

 echo 'Row inserted';

 exit;

}

?>



<html>

<head>

<title>Data submission - db version</title>

</head>

<body>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Name: <input name="name" /><br />

Address: <input name="address" /><br />

Postcode: <input name="postcode" /><br />

Telno: <input name="telephone" /><br />

Email: <input name="email" /><br />

Picture:<br><input type="file" name="picture" size"80" /><br />



<br><input type="submit" name="submitted" value="Submit" ></form><br>



</body>

</html>



Posted by admin (Graham Ellis), 24 March 2007
You need to look at move_uploaded_file to move the image you've uploaded to somewhere you can read it, then read it into a string.  And don't strip_tags out of the picture in any way as any binary data that looks like a tag would get damaged  

Here's an image upload script that I wrote showing the bare bones ...

Code:
<?php
#%% Image upload script
if ($_REQUEST[completed] == 1) {
       $newname = uniqid("whc").".jpg";
       move_uploaded_file($_FILES['mailfile']['tmp_name'],
               "../robdemo/$newname");
} ?>
<html>
<head><title>Upload page</title></head>
<body><h1>Image Uploader</h1>

<?php if ($_REQUEST[completed] != 1) { ?>
<b>Please upload an image</b><br>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=1500000>
<input type=hidden name=completed value=1>
Choose file to send: <input type=file name=mailfile> and
<input type=submit></form>
<?php } else { ?>
<b>Yum, Yum. I enjoyed that</b>
<?php } ?>
<hr>
Copyright, etc
</body></html>




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho