By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Uploading mutiple binary files to single row in Mysql with Php

P: 41
I need a little help figuring this one out. I have a script that I've modified to post mutiple binary files into a single row but instead of copying the indiviuals files it rewrites the first file to all the other columns in the row... Could someone help me with this . Below is the complete script...and form..

[PHP]
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] && $_FILES['audiofile1']['size'] && $_FILES['audiofile2']['size'] && $_FILES['audiofile3']['size'] && $_FILES['audiofile4']['size'] && $_FILES['audiofile5']['size'] && $_FILES['audiofile6']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
$fileName1 = $_FILES['audiofile1']['name'];
$tmpName1 = $_FILES['audiofile1']['tmp_name'];
$fileSize1 = $_FILES['audiofile1']['size'];
$fileType1 = $_FILES['audiofile1']['type'];

$fp = fopen($tmpName, 'r');
$audiofile1 = fread($fp, filesize($tmpName));
$audiofile1 = addslashes($audiofile1);
fclose($fp);
$fileName2 = $_FILES['audiofile2']['name'];
$tmpName2 = $_FILES['audiofile2']['tmp_name'];
$fileSize2 = $_FILES['audiofile2']['size'];
$fileType2 = $_FILES['audiofile2']['type'];

$fp = fopen($tmpName, 'r');
$audiofile2 = fread($fp, filesize($tmpName));
$audiofile2 = addslashes($audiofile2);
fclose($fp);
$fileName3 = $_FILES['audiofile3']['name'];
$tmpName3 = $_FILES['audiofile3']['tmp_name'];
$fileSize3 = $_FILES['audiofile3']['size'];
$fileType3 = $_FILES['audiofile3']['type'];

$fp = fopen($tmpName, 'r');
$audiofile3 = fread($fp, filesize($tmpName));
$audiofile3 = addslashes($audiofile3);
fclose($fp);
$fileName4 = $_FILES['audiofile4']['name'];
$tmpName4 = $_FILES['audiofile4']['tmp_name'];
$fileSize4 = $_FILES['audiofile4']['size'];
$fileType4 = $_FILES['audiofile4']['type'];

$fp = fopen($tmpName, 'r');
$audiofile4 = fread($fp, filesize($tmpName));
$audiofile4 = addslashes($audiofile4);
fclose($fp);
$fileName5 = $_FILES['audiofile5']['name'];
$tmpName5 = $_FILES['audiofile5']['tmp_name'];
$fileSize5 = $_FILES['audiofile5']['size'];
$fileType5 = $_FILES['audiofile5']['type'];

$fp = fopen($tmpName, 'r');
$audiofile5 = fread($fp, filesize($tmpName));
$audiofile5 = addslashes($audiofile5);
fclose($fp);
$fileName6 = $_FILES['audiofile6']['name'];
$tmpName6 = $_FILES['audiofile6']['tmp_name'];
$fileSize6 = $_FILES['audiofile6']['size'];
$fileType6 = $_FILES['audiofile6']['type'];

$fp = fopen($tmpName, 'r');
$audiofile6 = fread($fp, filesize($tmpName));
$audiofile6 = addslashes($audiofile6);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$fileName1 = addslashes($fileName1);
$fileName2 = addslashes($fileName2);
$fileName3 = addslashes($fileName3);
$fileName4 = addslashes($fileName4);
$fileName5 = addslashes($fileName5);
$fileName6 = addslashes($fileName6);
}
include 'config.php';
include 'opendb.php';

$query = "INSERT INTO audio (name, size, type, content, name1, size1, type1, audiofile1, name2, size2, type2, audiofile2, name3, size3, type3, audiofile3, name4, size4, type4, audiofile4, name5, size5, type5, audiofile5, name6, size6, type6, audiofile6 ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileName1', '$fileSize1', '$fileType1', '$audiofile1', '$fileName2', '$fileSize2', '$fileType2', '$audiofile2', '$fileName3', '$fileSize3', '$fileType3', '$audiofile3', '$fileName4', '$fileSize4', '$fileType4', '$audiofile4', '$fileName5', '$fileSize5', '$fileType5', '$audiofile5', '$fileName6', '$fileSize6', '$fileType6', '$audiofile6')";

mysql_query($query) or die('Error, query failed');
include 'closedb.php';

echo "<br>File $fileName uploaded<br> File $fileName1 uploaded<br>File $fileName2 uploaded<br>File $fileName3 uploaded<br>File $fileName4 uploaded<br>File $fileName5 uploaded<br>File $fileName6 uploaded<br>";
}
?>
[/PHP]
and heres the HTML Form
[HTML]
<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="8888000000">
<input name="userfile" type="file" id="userfile">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile1" type="file" id="audiofile1">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile2" type="file" id="audiofile2">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile3" type="file" id="audiofile3">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile4" type="file" id="audiofile4">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile5" type="file" id="audiofile5">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile6" type="file" id="audiofile6">
</td>
</tr>
<tr>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
[/HTML]

I've been playing with this for about a week and for some reason I can't get it to work..Any help would be greatly appreciated!
Nov 27 '06 #1
Share this Question
Share on Google+
10 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Why are you using the same statement for every to be uploaded file?
Expand|Select|Wrap|Line Numbers
  1. $fp = fopen($tmpName, 'r');
  2.  
Ronald :cool:
Nov 27 '06 #2

P: 41
I assumed the statement was required for each of the files that are being uploaded to the db.
Nov 27 '06 #3

P: 41
Ronald," after reading your post I made a modification to the script and manage to figure out how to get mutiple files uploaded using this shorten version of the orginal post
However another issue arose!
[PHP]
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size']&& $_FILES['audiofile1']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);

$fileName1 = $_FILES['audiofile1']['name1'];
$tmpName1 = $_FILES['audiofile1']['tmp_name'];
$fileSize1 = $_FILES['audiofile1']['size1'];
$fileType1 = $_FILES['audiofile1']['type1'];

$fp = fopen($tmpName1, 'r');
$audiofile1 = fread($fp, filesize($tmpName));
$audiofile1 = addslashes($audiofile1);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$fileName1 = addslashes($fileName1);
}
include 'config.php';
include 'opendb.php';

$query = "INSERT INTO audio (name, size, type, content, name1, size1, type1, audiofile1 ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileName1', '$fileSize1', '$fileType1', '$audiofile1')";

mysql_query($query) or die('Error, query failed');
include 'closedb.php';

echo "<br>File $fileName uploaded<br>File $fileName1 uploaded<br>";
}
?>
[/PHP]

And the html form looks like this
[HTML]
<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="8000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8000000">
<input name="audiofile1" type="file" id="audiofile1">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
[/HTML]

the only problem is that none of the atribute information is being store in the database for the audio file1. The file name1, size1, type1 are blank just the binary data is being stored.

Thanks again for your help..
Nov 28 '06 #4

P: 41
And as far as RTFM being almost extinct. I think most just don't have time with fast pace of life and all but here something for your amusement! Here my first website no training, no mentor's, nothing but a desire to learn..I know this doesn't come close what others can do but I'll get there one day..
[HTML]
http://www.atlantasundergroundhits.com/
[/HTML]

best veiwed in internet explorer..

Thanks again for you help..
RTFM "lol"
Nov 28 '06 #5

ronverdonk
Expert 2.5K+
P: 4,258
Great site!
However, you still have an erroneous statements [php]$audiofile1 = fread($fp, filesize($tmpName));
$tmpName1 = $_FILES['audiofile1']['tmp_name'];[/php]

Ronald :cool:
Nov 28 '06 #6

P: 41
I'll check into this today when I get home.

Thanks Ronald.
Nov 28 '06 #7

P: 41
It took me about 4 hours but I figured it out here's the PHP I took into account your recommendations..

[PHP]
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] && $_FILES['audiofile1']['size'] && $_FILES['audiofile2']['size'] && $_FILES['audiofile3']['size'] && $_FILES['audiofile4']['size'] && $_FILES['audiofile5']['size'] && $_FILES['audiofile6']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fileName1 = $_FILES['audiofile1']['name'];
$tmpName1 = $_FILES['audiofile1']['tmp_name'];
$fileSize1 = $_FILES['audiofile1']['size'];
$fileType1 = $_FILES['audiofile1']['type'];

$fileName2 = $_FILES['audiofile2']['name'];
$tmpName2 = $_FILES['audiofile2']['tmp_name'];
$fileSize2 = $_FILES['audiofile2']['size'];
$fileType2 = $_FILES['audiofile2']['type'];

$fileName3 = $_FILES['audiofile3']['name'];
$tmpName3 = $_FILES['audiofile3']['tmp_name'];
$fileSize3 = $_FILES['audiofile3']['size'];
$fileType3 = $_FILES['audiofile3']['type'];

$fileName4 = $_FILES['audiofile4']['name'];
$tmpName4 = $_FILES['audiofile4']['tmp_name'];
$fileSize4 = $_FILES['audiofile4']['size'];
$fileType4 = $_FILES['audiofile4']['type'];

$fileName5 = $_FILES['audiofile5']['name'];
$tmpName5 = $_FILES['audiofile5']['tmp_name'];
$fileSize5 = $_FILES['audiofile5']['size'];
$fileType5 = $_FILES['audiofile5']['type'];

$fileName6 = $_FILES['audiofile6']['name'];
$tmpName6 = $_FILES['audiofile6']['tmp_name'];
$fileSize6 = $_FILES['audiofile6']['size'];
$fileType6 = $_FILES['audiofile6']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
$fp = fopen($tmpName1, 'r');
$audiofile1 = fread($fp, filesize($tmpName1));
$audiofile1 = addslashes($audiofile1);
$fp = fopen($tmpName2, 'r');
$audiofile2 = fread($fp, filesize($tmpName2));
$audiofile2 = addslashes($audiofile2);
$fp = fopen($tmpName3, 'r');
$audiofile3 = fread($fp, filesize($tmpName3));
$audiofile3 = addslashes($audiofile3);
$fp = fopen($tmpName4, 'r');
$audiofile4 = fread($fp, filesize($tmpName4));
$audiofile4 = addslashes($audiofile4);
$fp = fopen($tmpName5, 'r');
$audiofile5 = fread($fp, filesize($tmpName5));
$audiofile5 = addslashes($audiofile5);
$fp = fopen($tmpName6, 'r');
$audiofile6 = fread($fp, filesize($tmpName6));
$audiofile6 = addslashes($audiofile6);

fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$fileName1 = addslashes($fileName1);
$fileName2 = addslashes($fileName2);
$fileName3 = addslashes($fileName3);
$fileName4 = addslashes($fileName4);
$fileName5 = addslashes($fileName5);
$fileName6 = addslashes($fileName6);

}
include 'config.php';
include 'opendb.php';

$query = "INSERT INTO audio (name, size, type, content, name1, size1, type1, audiofile1, name2, size2, type2, audiofile2, name3, size3, type3, audiofile3, name4, size4, type4, audiofile4, name5, size5, type5, audiofile5, name6, size6, type6, audiofile6 ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileName1', '$fileSize1', '$fileType1', '$audiofile1', '$fileName2', '$fileSize2', '$fileType2', '$audiofile2', '$fileName3', '$fileSize3', '$fileType3', '$audiofile3', '$fileName4', '$fileSize4', '$fileType4', '$audiofile4', '$fileName5', '$fileSize5', '$fileType5', '$audiofile5', '$fileName6', '$fileSize6', '$fileType6', '$audiofile6')";

mysql_query($query) or die('Error, query failed');
include 'closedb.php';

echo "<br>File $fileName uploaded<br> File $fileName1 uploaded<br>File $fileName2 uploaded<br>File $fileName3 uploaded<br>File $fileName4 uploaded<br>File $fileName5 uploaded<br>File $fileName6 uploaded<br>File $fileName7 uploaded<br>";
}
?>
[/PHP]

the form

[HTML]
<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="8888000000">
<input name="userfile" type="file" id="userfile">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile1" type="file" id="audiofile1">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile2" type="file" id="audiofile2">
</td>
</tr>
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile3" type="file" id="audiofile3">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile4" type="file" id="audiofile4">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="888000000">
<input name="audiofile5" type="file" id="audiofile5">
</td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="8888000000">
<input name="audiofile6" type="file" id="audiofile6">
</td>
</tr>
<tr>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
[/HTML]

The script uploads mutiple file with all the atribute information...
But I came across another issue when attempting upload a audio file and large images the blob feilds only allow 64 kilobytes to written to db. I think I
read acorss some documentation that said mysql by default only allows for 1 mega byte to be uploaded.. any idea's

Thanks for your help Ronald
great examples
Nov 29 '06 #8

ronverdonk
Expert 2.5K+
P: 4,258
The following are the maximum sizes for each BLOB field:

- TINYBLOB = 255 bytes
- BLOB = 65535 bytes
- MEDIUMBLOB = 16777215 bytes
- LONGBLOB = 4294967295 bytes

Robald :cool:
Nov 29 '06 #9

bishwadeep
P: 12
I think you have copied the code for each files
for eg: $tmpName is used for first file and for other files you have used the same
$tmpName
I mean There should be $tmpName1 in place of bold parts and similartly to to tmpName2,3,4,5,6:

$fileName1 = $_FILES['audiofile1']['name'];
$tmpName1 = $_FILES['audiofile1']['tmp_name'];
$fileSize1 = $_FILES['audiofile1']['size'];
$fileType1 = $_FILES['audiofile1']['type'];

$fp = fopen($tmpName, 'r');
$audiofile1 = fread($fp, filesize($tmpName));
$audiofile1 = addslashes($audiofile1);
fclose($fp);

Hope it is the right error
Nov 30 '06 #10

P: 41
Ronald that did the trick I was able to successfully upload audio files....

Now on to the next task....

Thanks again
Nov 30 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.