Skip to main content
April 9, 2009
Question

HELP!! How to upload image into database

  • April 9, 2009
  • 2 replies
  • 3225 views

Hello.

I would like to ask how to use file field in Dreamweaver to upload an image?? As far as I know, we just need to use database linking which store the filename of image into database. I really dont know how to start. Please help or if someone can post the example script here it would really help.

This topic has been closed for replies.

2 replies

April 14, 2009

Hi... I have found out one simple upload function to store an image. Finally!! Below is the code.

$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO upload2 (name, type, `size`, `path`) VALUES ('$fileName', '$fileType','$fileSize', '$filePath')",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['type'], "text"),
                       GetSQLValueString($_POST['size'], "int"),
                       GetSQLValueString($_POST['path'], "text"));

  mysql_select_db($database_project1, $project1);
  $Result1 = mysql_query($insertSQL, $project1) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
  echo "<br>Files uploaded<br>";
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
Upload image using database.
<form action="<?php echo $editFormAction; ?>" name="form" method="POST" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
<p>
  <label></label>
  <input name="name" type="hidden" id="name" value=".$fileName." />
  <input name="type" type="hidden" id="type" value=".$fileType." />
  <input name="size" type="hidden" id="size" value=".$fileSize." />
  <input name="path" type="hidden" id="path" value=".$filePath." />
  <input type="hidden" name="MM_insert" value="form" />
</p>
</form>

DwFAQ
Participating Frequently
April 14, 2009

There is no restriction on file type in your upload script so someone could upload an .exe file and compromise your database!

Make sure you have restriction to the page and only allow upload from trusted users. Stay Secure!

You can also check out the file type restriction code in the w3schools.com link I provided in my previous reply.

April 14, 2009

Thanks in advance. YA, I will try to look through the coding there and try n error again..

DwFAQ
Participating Frequently
April 9, 2009

Heya,

Get something like Adobe Developers Toolbox and add a file upload server behavior to your form.

Here is a discussion which shows how to do it using ADDT you can download the free trial of Adobe Developers Toolbox and see if it works for you like it has for many others.

Hope that helps!

April 9, 2009

Do you have any other options instead of buying the ADDT?

DwFAQ
Participating Frequently
April 9, 2009

Learn how to code and do it yourself is the "other option".