Jump to content

help with picture upload


laura_richer

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/195463-help-with-picture-upload/
Share on other sites

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)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.