Connecting Tech Pros Worldwide Forums | Help | Site Map

Handling Multiple File Uploads into Database with single INSERT INTO???

jenkinsloveschicken's Avatar
Member
 
Join Date: Dec 2006
Posts: 56
#1: Jan 15 '08
I am developing a administrative backend for our sales dept website. They are using admin forms to manipulate what is displayed for the end user on the site. I am running into a problem with a form design. The admin user needs to be able to assign both an image and a corresponding PowerPoint/Word/etc document to be downloaded once the end-user selects it.

I am able to get the image in and out of the database no problem, but I am not able to get the ppt to upload.

My submission form code is as follows(ugly I know, will clean up once I get it to work :P)

<div id="Layer1">
<form enctype="multipart/form-data" action=".../mxaddi_uc.php" method="post">
<div id="Layer3" style="font-family:'Trebuchet MS'">
<h4><font color="#FFFFFF" size="2">Please enter the title</font></h4>
</div>
<input type="text" name="title" value="" maxlength="120" width="30"/>
<br />
<h4><font color="#FFFFFF" size="2" style="font-family:'Trebuchet MS'">Please enter the contest description</font></h4>
<textarea name="description" rows="3"></textarea><br />

<div id="Layer2" style="font-family:'Trebuchet MS'">
<h4><font color="#FFFFFF" size="2">Please select image file to upload</font></h4>
</div>
<p>&nbsp;</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
</p>
<p>
<input name="userfile" type="file" />
</p>

<h4><font color="#FFFFFF" size="2" style="font-family:'Trebuchet MS'">Please select presentation file to upload</font></h4>
<p>
<input name="presentationfile" type="file" />
</p>
<p>
<input name="submit" type="submit" value="Submit" />
</p>

</form>
</div>

The receiving form code is as follows:

<?php
session_start();
require ('dbconfig.php');
$login = false;
$link = mysql_connect('localhost', $dbuser, $dbpass);
$dbname = 'mydb';
$db_selected = mysql_select_db($dbname, $link);

$title = $_POST['title'];
$description = $_POST['description'];

$presentationFileName = addslashes (file_get_contents($_FILES['presentationfile']['tmp_name']));

$imgFileName = addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

$stamp = mktime(date("h"), date("m"), date("s"), date("m"), date("d"), date("Y"));


$inc_update = "INSERT INTO MAX_Incentive (AutoID, MMI_heading, MMI_content, MMI_description, MMI_presentation) values ('$stamp', '$title', '$imgFileName', '$description', '$presentationFileName')";
$updatemov = mysql_query($inc_update);

?>

-$imgFileName is longblob data type
-$presentationFileName is longblob data type

This is new territory for me, I'm usually strictly data and reporting pages, but I have been tasked with re-writing this system. I would really appreciate if some of you gurus could lend a n00b a hand.

Thanks in advance,

Jenkins

MarkoKlacar's Avatar
Expert
 
Join Date: Aug 2007
Location: Stockholm, Sweden
Posts: 294
#2: Jan 15 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Hi,

from looking at your code it seems you don't get the file (ppt) only the name.
Other than that I can't see any major flaws.

Can you try getting the file content for the ppt and let us know?

Cheers
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,940
#3: Jan 15 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Please read the forum posting guidelines

Use code tags!

It makes things easier for us to read; if things arent easy to read we're not going to take the time to sit here and decipher it.
jenkinsloveschicken's Avatar
Member
 
Join Date: Dec 2006
Posts: 56
#4: Jan 15 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Re-posting with code tags:
Expand|Select|Wrap|Line Numbers
  1. <div id="Layer1">
  2. <form enctype="multipart/form-data" action=".../mxaddi_uc.php" method="post">
  3. <div id="Layer3" style="font-family:'Trebuchet MS'">
  4. <h4><font color="#FFFFFF" size="2">Please enter the title</font></h4>
  5. </div>
  6. <input type="text" name="title" value="" maxlength="120" width="30"/>
  7. <br />
  8. <h4><font color="#FFFFFF" size="2" style="font-family:'Trebuchet MS'">Please enter the contest description</font></h4>
  9. <textarea name="description" rows="3"></textarea><br />
  10.  
  11. <div id="Layer2" style="font-family:'Trebuchet MS'">
  12. <h4><font color="#FFFFFF" size="2">Please select image file to upload</font></h4>
  13. </div>
  14. <p>&nbsp;</p>
  15. <p>
  16. <input name="userfile" type="file" />
  17. </p>
  18.  
  19. <h4><font color="#FFFFFF" size="2" style="font-family:'Trebuchet MS'">Please select presentation file to upload</font></h4>
  20. <p>
  21. <input name="presentationfile" type="file" />
  22. </p>
  23. <p>
  24. <input name="submit" type="submit" value="Submit" />
  25. </p>
  26.  
  27. </form>
  28. </div>
  29.  
  30.  
The receiving form code is as follows:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. require ('dbconfig.php');
  4. $login = false;
  5. $link = mysql_connect('localhost', $dbuser, $dbpass);
  6. $dbname = 'mydb';
  7. $db_selected = mysql_select_db($dbname, $link);
  8.  
  9. $title = $_POST['title'];
  10. $description = $_POST['description'];
  11.  
  12. $presentationFileName = addslashes (file_get_contents($_FILES['presentationfile']['tmp_name']));
  13.  
  14. $imgFileName = addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
  15.  
  16. $stamp = mktime(date("h"), date("m"), date("s"), date("m"), date("d"), date("Y"));
  17.  
  18.  
  19. $inc_update = "INSERT INTO MAX_Incentive (AutoID, MMI_heading, MMI_content, MMI_description, MMI_presentation) values ('$stamp', '$title', '$imgFileName', '$description', '$presentationFileName')";
  20. $updatemov = mysql_query($inc_update);
  21.  
  22. ?>
  23.  
Again, MMI_content and MMI_presentation are longblob data types in the table. In response to the first reply, I am getting the actual image into the database using the same method as the ppt. I'm just not sure what the difference is, or if there is a technical limitation on this action. I have researched php.net and others, and all point back to the syntax I am using.



Any insight would be great!

Thanks,
Jenkins
MarkoKlacar's Avatar
Expert
 
Join Date: Aug 2007
Location: Stockholm, Sweden
Posts: 294
#5: Jan 16 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Hi,

do you get any error messages, the code looks fine to me (maybe I'm blind...).
jenkinsloveschicken's Avatar
Member
 
Join Date: Dec 2006
Posts: 56
#6: Jan 17 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Well not sure at this point. I have tried a different method as I checked my php.info script and all of the allotted file size related settings are correct. I was able to load ppt files that were below 512kb and write them to my server. I think I have isolated the problem to the LimitRequestBody setting of the php.conf file in the httpd directory. I am working with my local IT on this. Hopefully they will find a solution.

In the meantime, I have tried my hand at just parsing the file and writing it directly to the server without the db intervention with varying degrees of success. My goal is not to have to deal with server file permissions so that this system can function in a more autonomous fashion as I will be leaving this company in the next few months.

My line of thinking is to just create a single record and store the BLOB data associated with each sales contest/incentive entry added. This way the sales personnel can directly control the content of their pages without me having to worry about my app having to worry about file permissions on the server.

I may be overthinking this or not thinking it through enough. This isn't usually my bag of tea, but our local programmers have some resource constraining projects right now and I decided to lend some help. Most of my stuff is table based reporting, data parsing, limited automation, etc.

I am more or less looking for direction to take at this point if any of you more experienced guys have any ideas.

Thanks for all the time and consideration.

BTW: Here is some updated code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. session_start();
  4. require ('dbconfig.php');
  5. $login = false;
  6. $link = mysql_connect('localhost', $dbuser, $dbpass);
  7. $dbname = 'sprint-tech';
  8. $db_selected = mysql_select_db($dbname, $link);
  9.  
  10.     $title = $_POST['title'];
  11.     $description = $_POST['description'];
  12.  
  13.     if (isset($_FILES['userfile'])) {
  14.  
  15.         $imgFileName = addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
  16.         }
  17.         else
  18.         {
  19.         $imgFileName = '';
  20.         }
  21.  
  22.  
  23.     $presentationFileName = addslashes (file_get_contents($_FILES['presentationfile']['tmp_name']));
  24.     $presentationFileType = $_FILES['presentationfile']['type'];
  25.  
  26.     if ($presentationFileType <> '') {
  27.  
  28.         switch ($presentationFileType) {
  29.             case "application/vnd.ms-powerpoint":
  30.                 $fileExt = ".ppt";
  31.                 break;
  32.             case "application/msword":
  33.                 $fileExt = ".doc";
  34.                 break;
  35.             case "application/vnd.ms-excel":
  36.                 $fileExt = ".xls";
  37.                 break;
  38.                 }
  39.             }
  40.  
  41.     $stamp = mktime(date("h"), date("m"), date("s"), date("m"), date("d"), date("Y"));
  42.     $fileName = $stamp . $fileExt;
  43.  
  44.     $createVar = file_put_contents("documents/$fileName", "$presentationFileName");    
  45.     echo $fileName;
  46.  
  47.     $inc_update = "INSERT INTO MAX_Incentive (AutoID, MMI_heading, MMI_content, MMI_description, MMI_presentation, MMI_prestype, MMI_ext) values ('$stamp', '$title', '$imgFileName', '$description', '$presentationFileName', '$presentationFileType', '$fileExt')";
  48.     $updatemov = mysql_query($inc_update);
  49.  
  50.  
  51. ?>
  52.  
  53.  
-Jenkins
jenkinsloveschicken's Avatar
Member
 
Join Date: Dec 2006
Posts: 56
#7: Jan 17 '08

re: Handling Multiple File Uploads into Database with single INSERT INTO???


Please close this thread. I have changed the method of handling the file uploads.

-Jenkins
Reply