Author Topic: help with picture upload  (Read 165 times)

0 Members and 1 Guest are viewing this topic.

Offline laura_richerTopic starter

  • Irregular
  • Posts: 1
    • View Profile
help with picture upload
« on: March 16, 2010, 01:14:39 PM »
hi everyone,
Im pretty new to PHP coding and im having trouble writing the code to upload pictures to a mysql database.
the code i have so far is:

The upload form:

<p>Upload Image</p>
<form action="picupload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="picfile" type="file" id="picfile">
</td>
<td>
<input type="submit" name="submit" value="Submit" />
</td>
</tr>
</table>
</form>

Then the processing on the next page:

<?php
if(isset($_POST['upload']) && $_FILES['picfile']['size'] > 0)
{
$name = $_FILES['picfile']['name'];
$temp  = $_FILES['picfile']['tmp_name'];
$size = $_FILES['picfile']['size'];
$type = $_FILES['picfile']['type'];

echo $name;
echo $temp;
echo $size;
echo $type;

$fp      = fopen($temp, 'r');
$content = fread($fp, filesize($temp));
$content = addslashes($content);
fclose($fp);
}


$host="localhost";
$user="myusername";
$pass="mypassword";
mysql_connect($host,$user,$pass);
mysql_select_db($user);

$query= "insert into photo values (NOT NULL, '".$name."','".$type."','".$size."','".$content."')";
$result=mysql_query($query);

if(mysql_affected_rows()==1){                     

   echo '<p>File '.$name.' uploaded</p>';
}else{
   echo '<p>Error uplaoding</p>';
}

?>

It says the file has uploaded but in the mysql database the file size is 0 and the 'name' and 'type' column are empty and the content columm says BLOB-0B

thanks for the help in advance.
laura

Offline nafetski

  • Enthusiast
  • Posts: 270
    • View Profile
Re: help with picture upload
« Reply #1 on: March 16, 2010, 01:17:56 PM »
Here is the thing.

(People will disagree with me on this)

It's not a good idea to store your pictures in a MySQL database.  It's a good idea to store the file on the filesystem, and store the path and properties (width, height, size, etc) in your database...but I would almost NEVER recommend storing your image as binary data in the DB.

Come up with a directory structure that can handle a multitude of images, and a naming convention that won't cause collisions  (maybe username_time.jpg)

Dev Environment: Mac - OSX Snow Leopard / Eclipse / Kohana PHP Framework
Job: Sr Software Developer: (Large scale enterprise)
Notice:  Most of my forum posts I write on my iPhone while taking a dump.  This means that I don't test most of my code, and I might sound like I'm impatient...really I'm just busy punching a grumpy
Also: I've hit Google so many times it's asking for a divorce