Page 1 of 1

image upload to mysql

Posted: Mon Jun 08, 2009 4:49 pm
by ddarsow
I am trying to upload images to a mysql database via a php form. All of the data uploads...except the image file which stays at 0kb in the db. My code is as follows: Any help? I am pretty sure it is some silly little thing.

The add image form:

Code: Select all

            <h1>Upload a New Image</h1>         
            <p>To add a new image, simply fill in the form below and click on the "Upload&nbsp;Image" button located at the bottom of this page. To upload additional images for the property, simply repeat the process for each image.</p>
            <p>
            <form class="mid" action="insert_img.php" method="post" onsubmit="this.submit(); this.reset(); return false" enctype="multipart/form-data">
<strong>MLS Number: (REQUIRED!)</strong> <input type="text" name="mls" size="30"><br><br>
<strong>IMAGE:</strong> <input name="image" type="file"><br><br>
<strong>Thumbnail?:</strong> 
    <SELECT NAME="tn">
        <OPTION>N</OPTION>
        <OPTION>Y</OPTION>
    </SELECT><br><br>
<strong>Alt Text:</strong> <input type="text" name="alt" size="30"><br><br>
<strong>Image Title:</strong> <input type="text" name="title" size="60"><br><br>
<br><input type="Submit" value="Upload Image">&nbsp;<input type="reset" value="clear form">
</form>
            </p>
The insert script:

Code: Select all

<?
$username="hotspot3_hotspot";
$password="[redacted]";
$database="hotspot3_properties";
 
$mls=$_POST['mls'];
$tn=$_POST['tn'];
$alt=$_POST['alt'];
$title=$_POST['title'];
$image=$_POST['image'];
 
    $path = $_FILES['image']['tmp_name'];
    $name = $_FILES['image']['name'];
    $size = $_FILES['image']['size'];
    $type = $_FILES['image']['type'];
 
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$query = "INSERT INTO images VALUES ('{}','{$mls}','{$tn}','{$alt}','{$title}','{$name}','{$size}','{$type}','{$image}')";
mysql_query($query);
 
mysql_close();
 
?>
 
<script language="javascript">
location.replace("admin.php?page=addimage2");
</script>
the attached image shows what happens when submitting:
Screenshot.png
Screenshot.png (22.17 KiB) Viewed 826 times

Re: image upload to mysql

Posted: Mon Jun 08, 2009 5:34 pm
by akuji36
I think the field type should be blob.

Follow this link:

http://www.php-mysql-tutorial.com/wikis ... abase.aspx

Re: image upload to mysql

Posted: Mon Jun 08, 2009 6:27 pm
by ddarsow
akuji36 wrote:I think the field type should be blob.

Follow this link:

http://www.php-mysql-tutorial.com/wikis ... abase.aspx
it is. ...mediumblob more precisely.

Re: image upload to mysql

Posted: Mon Jun 08, 2009 7:36 pm
by mikemike
You're not actually inserting the image into the database there.

When you want to insert a file into the database then you need to open the file up, grab it's contents and then store the contents. You're trying to upload an empty variable.

Instead of just putting $_POST['image'] into the database you need to do something like this:

Code: Select all

$fp = fopen($_FILES['image']['tmp_name'], 'r'); // Open the file from the temporary directory
$content = fread($fp, filesize($_FILES['image']['tmp_name'])); // Read it into a variable
You can now insert $content into the BLOB field

Re: image upload to mysql

Posted: Tue Jun 09, 2009 11:59 am
by Benjamin
I have removed the password from your posted code.

Re: image upload to mysql

Posted: Tue Jun 09, 2009 12:26 pm
by mikemike
Ensure you're escaping $content too, or I could upload a file with some SQL injection code in and your script would open the file and insert the code.