Hello
I'm using mysql databases and php via dreamweaver for the scripting.

I have worked out how to submit images to a database and retrieve them.
How to just use the image_location to insert dynamic images into a webpage etc.
But I'm really struggling in working out how to just upload regular file types such as .pdf or .doc files to a database in a manner in which they can then be retrieved by customers looking at the website.

Ideally I just want to insert one more line to this form for .pdf file uploads:

<?php require_once('Connections/erinpubl_localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO eph_product (prod_id, prod_name, prod_level, prod_price, prod_status, image, thumbnail, prod_exercise) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['prod_id'], "text"),
                       GetSQLValueString($_POST['prod_name'], "text"),
                       GetSQLValueString($_POST['prod_level'], "text"),
                       GetSQLValueString($_POST['prod_price'], "text"),
                       GetSQLValueString($_POST['prod_status'], "text"),
                       GetSQLValueString($_POST['image'], "text"),
                       GetSQLValueString($_POST['thumbnail'], "text"),
                       GetSQLValueString($_POST['prod_exercise'], "text"));



  mysql_select_db($database_erinpubl_localhost, $erinpubl_localhost);
  $Result1 = mysql_query($insertSQL, $erinpubl_localhost) or die(mysql_error());
}
?>
<!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>Inserting Product Records</title>
</head>

<body>
<p>Insert a New Product Record</p>
<table width="529" border="1">
  <tr>
    <td>Please enter details of the new product, including full location URL of images.</td>
  </tr>
  <tr>
    <td height="230"><form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
      <table align="left">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Product ID:</td>
            <td><input type="text" name="prod_id" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Prod_name:</td>
            <td><input type="text" name="prod_name" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Prod_level:</td>
            <td><input type="text" name="prod_level" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Prod_price:</td>
            <td><input type="text" name="prod_price" value="" size="32" /></td>
          </tr>
          <td nowrap="nowrap" align="right">prod_status:</td>
            <td><input type="text" name="prod_status" value="" size="32" /></td>
          </tr>
           <tr valign="baseline">
            <td nowrap="nowrap" align="right">image:</td>
            <td><input name="image" type="text" value="" size="50" maxlength="80" /></td>
          </tr>
           <tr valign="baseline">
            <td nowrap="nowrap" align="right">thumbnail:</td>
            <td><input name="thumbnail" type="text" value="" size="50" maxlength="80" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">prod_exercise:</td>
            <td><input name="prod_exercise" type="text" value="" size="50" maxlength="80" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="Insert record" /></td>
          </tr>
          </table>
        <p>
          <input type="hidden" name="MM_insert" value="form1" />
        </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
    </form>
    <p>&nbsp;</p></td>
  </tr>
</table>
</body>
</html>

Recommended Answers

All 3 Replies

Here's the catch: When you put the full path in the input textfield, EVERYTIME your access the 'original' file. If your Wamp server is in C:\Wamp\www and you have an image stored in C:\img.jpg; how would you access your file once you upload your site to the server ??! Only the "link" to the file is inserted to the DB, not the file.
As for uploading files, a nice habit is to use the input file field as in >

<input type="file" name="image" id="imageid" />

Check this tutorial for more on file upload

Hi Mindster thanks for the response.
I'm starting again with a slightly simpler script. This time I've managed to return a pdf file. My issue now is that I actually want the list of pdf's to show?

<?php require_once('../Connections/erinpubl_localhost.php'); ?>
<?php
$errmsg = "";
 if (! @mysql_connect("localhost", "erinpubl", "7w8QVwmq4D")) {
        $errmsg = "Cannot connect to database";
        print ($errmsg);
        exit();
        }
@mysql_select_db("erinpubl_product");

// get the .pdf

$gotten = @mysql_query("select * from pdf order by pid desc limit 10");
$row = @mysql_fetch_assoc($gotten);
$bytes = $row[imgdata];
header("Content-type: application/pdf"); 
/* header('Content-disposition: attachment; filename="thing.pdf"'); */
print $bytes;
?> 

<!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>
</body>
</html>
<?php
mysql_free_result($rstest);
?>

Show the list of pdfs using a table and links using <a href="uploads/<?php $row?>">PDF</a>.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.