Page 1 of 1

insertion and retrieval of piture file

Posted: Fri Feb 14, 2003 11:08 am
by zia
Hello every body

I am seeking your help. Hope I will get.

Here is the scenario:

I have a mediumblob data type in mysql (3.23.54).
The portion of the form that takes the picture (jpg) like this :-

<form enctype="multipart/form-data" method="POST" action="Add_Edit_Teacher.php">
………………………………………………………………………………………………
………………………………………………………………………………………………
<input type="hidden" name="MAX_FILE_SIZE" value="900000">
<input type="file" name="pic_file" >
………………………………………………………………………………………………
………………………………………………………………………………………………
</form>

I need to insert the picture into the database and retrieve and show it in another page.

For insertion I have proceeded up to this point in Add_Edit_Teacher.php file: -
(Using PHP 4.3 with register_globals default off)

if ($_POST['submit'])
{
$db=mysql_connect("localhost","root","");
mysql_select_db("iict_student",$db);

if (is_uploaded_file($_FILES['pic_file']['tmp_name']))

copy($_FILES['pic_file']['tmp_name'], "c:\\".$_FILES['pic_file']['name']);

This has been OK. After submitting I got the picture in c:\

Now please help me 1) for insertion how I will proceed and 2) to retrieve and show the picture in another page what I will do?

Waiting for your reply.
Thank you in advance.

___ZIA

Posted: Fri Feb 14, 2003 11:41 am
by patrikG
You want to insert the image-file into the database? Why?
You would have to convert it to MIME-format to store it, then re-convert it to whatever the original format was when you read it out.
Imagine 50 images in your database with a maximum file-size (like the one you specified) of roughly 860kb. Sure, that's 40.3mb. But 50 images isn't much, is it? 100 = 403mb...
If you ever wanted to backup your database, you'd have to download one hell of a lot of data.

I find it much simpler if you keep a reference to the image in your database and have the file sitting in some directory on your server:

Code: Select all

<?php
include("dbconnection.inc.php");
if ($image!="none" && $image)
	&#123;
	if (filesize($image)<110000)
		&#123;
		copy($image,"../campaigns/images/".$imagename); 
		unlink($image);
                $insertIntoTable = mysql_query("insert into yourTable (imageName) values ($imagename) where condition='$condition'");
		$image="../campaigns/images/".$imagename;
		&#125;
	else 
		&#123;
		echo "The file $image has a size of ".filesize($imagename)." and is too big. Use the back-button of your browser to choose a different image.<br>The maximum filesize allowed is 100KB.";
		return;
		&#125;
	echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><title>Bla</title></head><br><body>Image uploaded</body'></html>";
	&#125;
	else
	&#123;echo "<p class=bluetext>Problem with image: $imagename</p>";&#125;
?>
That way you just link to it and let HTTP, HTML and the browser worry about formats and conversion. It also saves an immense amount of space in your database.