Handling Multiple File Uploads into Database with single INSERT INTO???  | Member | | Join Date: Dec 2006
Posts: 56
| | |
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> </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
|  | Expert | | Join Date: Aug 2007 Location: Stockholm, Sweden
Posts: 294
| | | 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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,940
| | | 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.
|  | Member | | Join Date: Dec 2006
Posts: 56
| | | re: Handling Multiple File Uploads into Database with single INSERT INTO???
Re-posting with code tags: -
<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> </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);
-
-
?>
-
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
|  | Expert | | Join Date: Aug 2007 Location: Stockholm, Sweden
Posts: 294
| | | 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...).
|  | Member | | Join Date: Dec 2006
Posts: 56
| | | 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: -
-
<?php
-
session_start();
-
require ('dbconfig.php');
-
$login = false;
-
$link = mysql_connect('localhost', $dbuser, $dbpass);
-
$dbname = 'sprint-tech';
-
$db_selected = mysql_select_db($dbname, $link);
-
-
$title = $_POST['title'];
-
$description = $_POST['description'];
-
-
if (isset($_FILES['userfile'])) {
-
-
$imgFileName = addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
-
}
-
else
-
{
-
$imgFileName = '';
-
}
-
-
-
$presentationFileName = addslashes (file_get_contents($_FILES['presentationfile']['tmp_name']));
-
$presentationFileType = $_FILES['presentationfile']['type'];
-
-
if ($presentationFileType <> '') {
-
-
switch ($presentationFileType) {
-
case "application/vnd.ms-powerpoint":
-
$fileExt = ".ppt";
-
break;
-
case "application/msword":
-
$fileExt = ".doc";
-
break;
-
case "application/vnd.ms-excel":
-
$fileExt = ".xls";
-
break;
-
}
-
}
-
-
$stamp = mktime(date("h"), date("m"), date("s"), date("m"), date("d"), date("Y"));
-
$fileName = $stamp . $fileExt;
-
-
$createVar = file_put_contents("documents/$fileName", "$presentationFileName");
-
echo $fileName;
-
-
$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')";
-
$updatemov = mysql_query($inc_update);
-
-
-
?>
-
-
-Jenkins
|  | Member | | Join Date: Dec 2006
Posts: 56
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|