image upload to mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ddarsow
Forum Newbie
Posts: 2
Joined: Mon Jun 08, 2009 4:39 pm

image upload to mysql

Post 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 825 times
Last edited by Benjamin on Tue Jun 09, 2009 11:58 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: image upload to mysql

Post by akuji36 »

I think the field type should be blob.

Follow this link:

http://www.php-mysql-tutorial.com/wikis ... abase.aspx
ddarsow
Forum Newbie
Posts: 2
Joined: Mon Jun 08, 2009 4:39 pm

Re: image upload to mysql

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: image upload to mysql

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: image upload to mysql

Post by Benjamin »

I have removed the password from your posted code.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: image upload to mysql

Post 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.
Post Reply