Reply
PHP - Multiple File Upload and Insert in MySQl
Old 10-28-2009, 04:37 AM PHP - Multiple File Upload and Insert in MySQl
Junior Talker

Posts: 2
Trades: 0
Hi,

I am new here and bit confused with one of my php projects.

Any help is highly appreciated.

My Problem :

I want users to upload multiple files at a time and store the data in mysql.

I have done this for one file, where in user can upload one file and the
data is processed and inserted to mysql...

My question is how can i do the same for multiple files.


My HTML Codes and php code to upload one file is as follows :

________Html Codes ____________________________

<html>

<head>

<title>New Page 1</title>
</head>

<body>
<form method="post" enctype="multipart/form-data" action="upload.php">
<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" size="20">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>

</html>

___________________PHP CODES _________________________

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['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);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("pj1") or die(mysql_error());
mysql_query("CREATE TABLE usrfiles(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
)")
or die(mysql_error());

$query = "INSERT INTO usrfiles (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');


echo "<br>File $fileName uploaded<br>";
echo "<br> New File Created with data<br>";


}
?>

_______________________END CODE_________________


Julie
julie517 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-28-2009, 05:19 AM Re: PHP - Multiple File Upload and Insert in MySQl
chrishirst's Avatar
Super Moderator

Posts: 22,245
Location: Blackpool. UK
Trades: 0
a foreach loop
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-28-2009, 05:19 AM Re: PHP - Multiple File Upload and Insert in MySQl
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
make the upload hanlder a function, and do an iteration over $_FILES[]:
PHP Code:
<?php
function dbInit(){
    
mysql_connect("localhost""root""") or die(mysql_error());
    
mysql_select_db("pj1") or die(mysql_error());
    
    
mysql_query("CREATE TABLE if not exists usrfiles(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
)"
) or die(mysql_error());
}

function 
handleUpload($index){
    if(isset(
$_FILES[$index]===false){
        return 
false;
    }
    
    
$fileName $_FILES[$index]['name'];
    
$tmpName $_FILES[$index]['tmp_name'];
    
$fileSize $_FILES[$index]['size'];
    
$fileType $_FILES[$index]['type'];
    
    
$fp fopen($tmpName'r');
    
$content fread($fpfilesize($tmpName));
    
$content addslashes($content);
    
fclose($fp);
    
    if(!
get_magic_quotes_gpc()){
        
$fileName addslashes($fileName);
    }
    
    if(
$fileSize>0){
        
dbInit();
        
$query "INSERT INTO usrfiles (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

        
mysql_query($query) or die('Error, query failed');
    
        echo 
"<br>File $fileName uploaded<br>";
        echo 
"<br> New File Created with data<br>";
    }
}

if(isset(
$_POST['upload'])){
    foreach(
$_FILES as $key=>$ar){
        
handleUpload($key);
    }


?>
This is just typed in notepad, live, so I don't guarantee there is no error, but you'll get the idea and the start point.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 10-28-2009, 02:09 PM Re: PHP - Multiple File Upload and Insert in MySQl
jim1228's Avatar
Super Talker

Posts: 147
Name: Jim
Location: Ohio
Trades: 0
Just a tip:

Use mysql_real_escape_string rather then addslashes
__________________
My Great Scripts
Custom DHTML/JavaScript Animation - Hire me for your next web project.
jim1228 is offline
Reply With Quote
View Public Profile Visit jim1228's homepage!
 
Old 10-30-2009, 02:07 AM Re: PHP - Multiple File Upload and Insert in MySQl
Junior Talker

Posts: 2
Trades: 0
I am still unable to get this done, have searched the net a lot but could
not find and proper codes for this.

Kindly help with the exact codes.

thanks

Julie
julie517 is offline
Reply With Quote
View Public Profile
 
Old 10-30-2009, 03:31 AM Re: PHP - Multiple File Upload and Insert in MySQl
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
That's what Tripy did, gave you the exact codes. Have you tried it?
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to PHP - Multiple File Upload and Insert in MySQl
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.11972 seconds with 13 queries