Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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
 
 
Register now for full access!
Old 10-28-2009, 05:19 AM Re: PHP - Multiple File Upload and Insert in MySQl
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,516
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
a foreach loop
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
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,621
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 offline
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
Extreme Talker

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

Use mysql_real_escape_string rather then addslashes
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
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
That's what Tripy did, gave you the exact codes. Have you tried it?
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 09-06-2011, 03:15 PM Re: PHP - Multiple File Upload and Insert in MySQl
Junior Talker

Posts: 1
Trades: 0
Quote:
Originally Posted by tripy View Post
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.
Hey Trippy,

That's a smart idea, i'm trying to do something similar but the form I'm using is quite long and the files need to be appended to the record the form is already creating. I'm going to look into it but any other thoughts and ideas would be appreciated.
Thx
fortressrealcap is offline
Reply With Quote
View Public Profile
 
Old 09-06-2011, 07:06 PM Re: PHP - Multiple File Upload and Insert in MySQl
Extreme Talker

Posts: 168
Trades: 0
If your not too proud to be borrow and steel, here is a nice system for uploading several files. I used it to upload 100 files all at once and it provides a status for each file that is uploaded.

http://webdeveloperplus.com/jquery/m...-using-jquery/
dgkindy is offline
Reply With Quote
View Public Profile
 
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.28602 seconds with 12 queries