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

uploading several docs at once

P: n/a
I have a meetings section I'm developing on our intranet. Using PHP/MySQL.
Meeting info and Meeting docs reside on 2 related tables in the db. Users
may want to upload anywhere from 1 to 10 or more documents to share/use
during a meeting presentation. What would be the most efficient way to
approach this? This is the logic I'm currently considering:

Page 1: Meeting Information input with link to a document upload page (this
page already exists as a link for 'New Meeting')
Page 2: Document upload page -
1. should I pass the mysql_insert_id() from the meeting ID on the
previous page or
2. provide a select box that lists last inserted meeting on top as well
as other meetings?
I'm considering this because a manager may want to upload a doc for
a meeting that they posted in the db earlier, but never got around to
uploading docs.
However, perhaps this should be an entirely different page to
upload docs for already posted meetings?
3. should this page have several upload file inputs? If so, how many is
reasonable?
Page 3: Upload confirmation with a link ("add another doc for this meeting")
that reloads the page as a sticky form if not enough links

Another possibility is to have it all on one page, but I can see that this
could get messy. I'm still new to PHP/MySQL so I have a hard time following
code that is used in only one page, but revealing only the portion
applicable. (i.e. if(submitted){do this} else {do that}. Seems like
sometimes it wants to show everything anyway.

Just fishing for ideas here...

Thanks,
Chris
Aug 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Well, I guess my question sounds stupid because no one answered it. I'm
fairly new to this, I do understand uploading multiple files, my concern is
getting them input into the database properly. I have no one to consult or
brainstorm with here (I am an intern teaching myself php/mysql). Although
there are many references out there - books, online, newsgroups, etc., - I
have seen several approaches. I'm on a tight deadline to complete this
project, so to save time re-writing code, I would just like some advice on
the direction I should go with this - am I on the right track? Is there
already a concept out there so I don't have to re-invent the wheel?

Thanks,
Chris

"Chris" <de************@centurytel.netwrote in message
news:ea*********@news01.intel.com...
>I have a meetings section I'm developing on our intranet. Using PHP/MySQL.
Meeting info and Meeting docs reside on 2 related tables in the db. Users
may want to upload anywhere from 1 to 10 or more documents to share/use
during a meeting presentation. What would be the most efficient way to
approach this? This is the logic I'm currently considering:

Page 1: Meeting Information input with link to a document upload page
(this page already exists as a link for 'New Meeting')
Page 2: Document upload page -
1. should I pass the mysql_insert_id() from the meeting ID on the
previous page or
2. provide a select box that lists last inserted meeting on top as well
as other meetings?
I'm considering this because a manager may want to upload a doc for
a meeting that they posted in the db earlier, but never got around to
uploading docs.
However, perhaps this should be an entirely different page to
upload docs for already posted meetings?
3. should this page have several upload file inputs? If so, how many
is reasonable?
Page 3: Upload confirmation with a link ("add another doc for this
meeting") that reloads the page as a sticky form if not enough links

Another possibility is to have it all on one page, but I can see that this
could get messy. I'm still new to PHP/MySQL so I have a hard time
following code that is used in only one page, but revealing only the
portion applicable. (i.e. if(submitted){do this} else {do that}. Seems
like sometimes it wants to show everything anyway.

Just fishing for ideas here...

Thanks,
Chris

Aug 7 '06 #2

P: n/a
>
"Chris" <de************@centurytel.netwrote in message
news:ea*********@news01.intel.com...
I have a meetings section I'm developing on our intranet. Using PHP/MySQL.
Meeting info and Meeting docs reside on 2 related tables in the db. Users
may want to upload anywhere from 1 to 10 or more documents to share/use
during a meeting presentation. What would be the most efficient way to
approach this? This is the logic I'm currently considering:

Page 1: Meeting Information input with link to a document upload page
(this page already exists as a link for 'New Meeting')
Page 2: Document upload page -
1. should I pass the mysql_insert_id() from the meeting ID on the
previous page or
2. provide a select box that lists last inserted meeting on top as well
as other meetings?
I'm considering this because a manager may want to upload a doc for
a meeting that they posted in the db earlier, but never got around to
uploading docs.
However, perhaps this should be an entirely different page to
upload docs for already posted meetings?
3. should this page have several upload file inputs? If so, how many
is reasonable?
Page 3: Upload confirmation with a link ("add another doc for this
meeting") that reloads the page as a sticky form if not enough links

Another possibility is to have it all on one page, but I can see that this
could get messy. I'm still new to PHP/MySQL so I have a hard time
following code that is used in only one page, but revealing only the
portion applicable. (i.e. if(submitted){do this} else {do that}. Seems
like sometimes it wants to show everything anyway.

Just fishing for ideas here...

Thanks,
Chris
Chris wrote:
Well, I guess my question sounds stupid because no one answered it. I'm
fairly new to this, I do understand uploading multiple files, my concern is
getting them input into the database properly.
Sorry you didn't get a reply, it didn't sound stupid, maybe its just
that questions with an error message and a some code to debug are
quicker and easier to answer than brainstorming

Anyway I've got a coffee and some time free.
I'm on a tight deadline to complete this
project, so to save time re-writing code, I would just like some advice on
the direction I should go with this - am I on the right track?
Yes I think you are.
Is there
already a concept out there so I don't have to re-invent the wheel?
Your plan to have seperate pages for adding meetings/uploading docs is
good and I'd do it that way too.

This way, like you said, the document upload page can be used when a
new meeting is added and also later when meetings have been added
already.

So.. have one document upload page and have two pages which will direct
the user (and submit a meeting id) to the documents upload page.

One way is when a new meeting is added - pass the insert_id to the
document upload page with something like '<a
href="/doc_upload?meet_id=' .mysql_insert_id() . '>'

The other way is after a meeting has already been added - have a
seperate 'choose meeting' page with just a select box of recently added
meetings which submits a meeting id to the document upload page. If the
<form method="get" action="doc_upload"and the select name is
'meet_id' then one document upload page can be used for both ways.
Another possibility is to have it all on one page, but I can see that this
could get messy.
Yup, makes an editor with syntax highlighting look like a plate by
damien hirst

3. should this page have several upload file inputs? If so, how many
is reasonable?
Not sure, now ask how long some string is! (Just kidding) I guess a few
isn't enough but dozens is too many.
Page 3: Upload confirmation with a link ("add another doc for this
meeting") that reloads the page as a sticky form if not enough links
I've seen both and both work fine.

For example the file upload page in Hotmail has one file input with two
submit buttons, one button labelled 'Ok upload file', the other button
labelled 'Ok and upload another' ...In a cpanel clone I've seen the
multiple file inputs method with one button labelled 'Upload files'. Go
with whichever method you think is easiest or best.

Personally I found the multiple file input method marginally more
usable and its probably easier for you because you could cut n paste
the code from example 38-3 on
http://www.php.net/manual/en/features.file-upload.php

Cheers
Tim Hunt

Aug 8 '06 #3

P: n/a
Hi Tim, thank you so much for getting back to me.

I've reviewed a few multiple file upload tutorials/guidelines, and have had
the best luck with the one I have posted below - I made adjustments as
needed.
The create meeting form works great - and the processing form (see below)
inputs all data as defined in the meetings table. It also recognizes the
$mtgid because it is printed out as part of the confirmation to the user.
It looks to see if someone checked the upload docs checkbox, so there will
be a heading with instructions to upload.

It is set up to get the number of uploads from a textbox on the file
submission form, then it creates all the input features for the number of
docs the user wants to upload. - becomes $num_files, but have hard coded it
for testing the file upload on the snippet. I get my confirmation printout,
but it never does insert the data into the database, nor does it pick up the
value of '$mtgid = mysql_insert_id()' which was created from submitting the
original form for listing a meeting in a meetings table. Have tried several
things to pass the value but haven't come up with the right one yet - it
would be the same for each upload as each doc will apply to the same
meeting.

I've been working on this particular document for 2 whole days - I think my
brain and my eyes just aren't in sync anymore.

Can you give this a look and see if anything jumps out as being so wrong
that the processing just won't work?
------------------------------
<?php
//set variables for meeting input:
$date = $_POST['mtgdate'];
$team = $_POST['team'];
$project = $_POST['project'];
$subject = $_POST['subject'];

if (isset($_POST['numfiles'])){
$num_files = $_POST['numfiles'];
} else {
$num_files = 1;
}

//check that form has been submitted
if (isset($_POST['addmeeting'])) { //begin check for submission

//insert data into meetings table
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meetings VALUES ('', '$date', '$team',
'$subject', '$project')";

//Confirm if data inserted

$success = mysql_query($doc_insert); //create variable to hold insert
values
$mtgid = mysql_insert_id(); //create variable for last inserted id

//check if meeting input is successful
if ($success) {
echo "Your ". $date . " meeting has been added to the database. <br />Your
meeting number is " .$mtgid .",
<br />keep this number handy for future reference. <br /><a
href='new.php'>Add another meeting.</a>";
} else {
echo "There was an error adding your meeting to the database, please try
again.";
}//end check if meeting successful -

if (isset($_POST['upload'])){ //check if file upload request

echo "Please use the form below to upload your meeting documents.<br />";

} //end check for upload request
}//end check if meeting added

---NOTE: works great up to here----

//upload directory.
$upload_dir = "docs/";
//number of files to upload.
$num_files = 2;

//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("Error: The directory <b>($upload_dir)</bdoesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("Error: The directory <b>($upload_dir)</bis NOT writable,
Please CHMOD (777)");
}

//if the form has been submitted, then do the upload process
if (isset($_POST['upload_form'])){

echo "<h3>Upload results:</h3>";

//do a loop for uploading files based on ($num_files) number of
files.
for ($i = 1; $i <= $num_files; $i++) {

//define variables to hold the values.
$new_file = $_FILES['file'.$i];
$file_name = $new_file['name'];
//to remove spaces from file name we have to replace it with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_tmp = $new_file['tmp_name'];
$file_size = $new_file['size'];
$title = $_POST['title'.$i];
$desc = $_POST['description'.$i];
#-----------------------------------------------------------#
# this code will check if the files was selected or not. #
#-----------------------------------------------------------#

if (!is_uploaded_file($file_tmp)) {
//print error message and file number.
echo "File $i: Not selected.<br>";
}else{
#-----------------------------------------------------------#
# this code check if file is Already EXISTS.
#
#-----------------------------------------------------------#

if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already
exists.<br>";
}else{
#-----------------------------------------------------------#
# this function will upload the files.
:) ;) cool #
#-----------------------------------------------------------#
if
(move_uploaded_file($file_tmp,$upload_dir.$file_na me)) {
echo "File $i: ($file_name)
Uploaded.<br>";
echo "File $i: ($title)<br>";
echo "File $i: ($desc)<br>";
//enter file data into database
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meeting_docs
VALUES ('', '$file_name', '$title', '$desc', '$mtgid')";
$success = mysql_query($doc_insert);
//create variable to hold insert values

}else{
echo "File $i: Failed to
upload.<br>";
}#end of (move_uploaded_file).

}#end of (file_exists).

}#end of (!is_uploaded_file).

}#end of (for loop).
# print back button.
echo "<a href=\"$_SERVER[PHP_SELF]\">add more documents</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form isn't submitted then show it.
}else{ ?>
<h3>Select files to upload!</h3>
Max file size = <?php $size_bytes / 1024 ?KB
<form method="post" action="<?php $_SERVER['PHP_SELF']?>"
enctype="multipart/form-data">
<?php // show the file input field based on($num_files).
for ($i = 1; $i <= $num_files; $i++) { ?>
File <?php echo $i ?>: <input type="file" name="file<?php
echo $i ?>"/><br />
<strong>Document Title:</strong>(Required - Max 50 characters):
<input type="text" size="30" maxlength="50" name="title<?php echo $i
?>"/><br />
<strong>Description:</strong>(Required - Max 150 characters):
<textarea cols="20\" rows="2" name="description<?php echo $i
?>"/></textarea><br />
<?php } ?>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $size_bytes
?>">
<input type="submit" name="upload_form" value="Upload Now!">
</form>
<? } //end check for upload submitted and form

//print copyright ;-)
echo"<p align=\"right\"><br>Script by: <a
href=\"http://www.maaking.com\">maaking.com</a></p>";

--------------------------------------------------------------
"Tim Hunt" <ti********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>Sorry you didn't get a reply, it didn't sound stupid, maybe its just
that questions with an error message and a some code to debug are
quicker and easier to answer than brainstorming

Anyway I've got a coffee and some time free.
> I'm on a tight deadline to complete this
project, so to save time re-writing code, I would just like some advice
on
the direction I should go with this - am I on the right track?

Yes I think you are.
>Is there
already a concept out there so I don't have to re-invent the wheel?

Your plan to have seperate pages for adding meetings/uploading docs is
good and I'd do it that way too.

This way, like you said, the document upload page can be used when a
new meeting is added and also later when meetings have been added
already.

So.. have one document upload page and have two pages which will direct
the user (and submit a meeting id) to the documents upload page.

One way is when a new meeting is added - pass the insert_id to the
document upload page with something like '<a
href="/doc_upload?meet_id=' .mysql_insert_id() . '>'

The other way is after a meeting has already been added - have a
seperate 'choose meeting' page with just a select box of recently added
meetings which submits a meeting id to the document upload page. If the
<form method="get" action="doc_upload"and the select name is
'meet_id' then one document upload page can be used for both ways.
Another possibility is to have it all on one page, but I can see that
this
could get messy.

Yup, makes an editor with syntax highlighting look like a plate by
damien hirst

3. should this page have several upload file inputs? If so, how many
is reasonable?

Not sure, now ask how long some string is! (Just kidding) I guess a few
isn't enough but dozens is too many.
Page 3: Upload confirmation with a link ("add another doc for this
meeting") that reloads the page as a sticky form if not enough links

I've seen both and both work fine.

For example the file upload page in Hotmail has one file input with two
submit buttons, one button labelled 'Ok upload file', the other button
labelled 'Ok and upload another' ...In a cpanel clone I've seen the
multiple file inputs method with one button labelled 'Upload files'. Go
with whichever method you think is easiest or best.

Personally I found the multiple file input method marginally more
usable and its probably easier for you because you could cut n paste
the code from example 38-3 on
http://www.php.net/manual/en/features.file-upload.php

Cheers
Tim Hunt

Aug 8 '06 #4

P: n/a

Chris wrote:
Hi Tim, thank you so much for getting back to me.

I've reviewed a few multiple file upload tutorials/guidelines, and have had
the best luck with the one I have posted below - I made adjustments as
needed.
The create meeting form works great - and the processing form (see below)
inputs all data as defined in the meetings table. It also recognizes the
$mtgid because it is printed out as part of the confirmation to the user.
It looks to see if someone checked the upload docs checkbox, so there will
be a heading with instructions to upload.
if
(move_uploaded_file($file_tmp,$upload_dir.$file_na me)) {
echo "File $i: ($file_name)
Uploaded.<br>";
echo "File $i: ($title)<br>";
echo "File $i: ($desc)<br>";
//enter file data into database
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meeting_docs
VALUES ('', '$file_name', '$title', '$desc', '$mtgid')";
$success = mysql_query($doc_insert);
//create variable to hold insert values

}else{
echo "File $i: Failed to
upload.<br>";
}#end of (move_uploaded_file).

}#end of (file_exists).

}#end of (!is_uploaded_file).

}#end of (for loop).
# print back button.
echo "<a href=\"$_SERVER[PHP_SELF]\">add more documents</a>";
Hi Chris

No I can't see what the problem is either.

I'm assuming its working like you expect apart until the part above
where the uploaded file's are moved then details recorded in the
meeting_docs table, is that right?

Will you try this debugging stuff.. Call mysql_error() after
mysql_query to see if the query syntax is at fault, print out
$doc_insert and try running the query by hand in phpmyadmin or mysql
cli, are you getting the expected number of "file $i: Uploaded br>"
messages?

I did wonder if the problem was inserting '' into the auto_increment
column, the mysql manual says only NULL or 0 will trigger an auto
increment - I've double checked the manual but I'm still not sure if ''
is treated the same as 0. It may not make a difference but would you
try INSERT ... VALUES(NULL, '$file_name'...) just in case.

Btw, top posting can make it hard for most people using newsreader
clients to follow the thread - could you put your reply to messages
underneath the previous message. Ta.

Cheers
Tim

Aug 9 '06 #5

P: n/a
Thanks again Tim,

It's working fine now. Here are my responses to your suggestions:

"Call mysql_error() after mysql_query to see if the query syntax is at
fault, print out
$doc_insert and try running the query by hand in phpmyadmin or mysql cli,
are you getting the expected number of "file $i: Uploaded br>"
messages?"

Wow - do I feel stupid - I had deleted one of the columns in the table, and
had forgotten about it -- so the query just had too many values. Something
so simple gets overlooked after you've been staring at it for so many
hours - thanks for reminding me to check the query directly.

"I did wonder if the problem was inserting '' into the auto_increment
column, the mysql manual says only NULL or 0 will trigger an auto
increment - I've double checked the manual but I'm still not sure if '' is
treated the same as 0. It may not make a difference but would you
try INSERT ... VALUES(NULL, '$file_name'...) just in case."

FYI - the '' seems to work as a NULL. I use it with all of my INSERT
queries The pk is auto-incremented in every case.

"Btw, top posting can make it hard for most people using newsreader clients
to follow the thread - could you put your reply to messages
underneath the previous message. Ta."

Sorry for toppost - a habit I developed from reading my personal messages -
I always know what I sent, so I want to see the response first.

I was also able to pass values for $mtgid and $num_files by rearranging the
elements (forms vs. form processing) then adding hidden fields into the
forms. However, I have noticed that the 'str_replace' that is supposed to
change any spaces in the file name to an underscore doesn't work. Any
suggestions for a method to make this work? Following is my final code for
anyone who is preparing a similar document:
<?php
//set variables for meeting input:
$date = $_POST['mtgdate'];
$team = $_POST['team'];
$project = $_POST['project'];
$subject = $_POST['subject'];

if (isset($_POST['numfiles'])){
$num_files = $_POST['numfiles'];
} else {
$num_files = 1;
}

//check that form has been submitted
if (isset($_POST['addmeeting'])) { //begin check for submission

//insert data into meetings table
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meetings VALUES ('', '$date', '$team',
'$subject', '$project')";

//Confirm if data inserted

$success = mysql_query($doc_insert); //create variable to hold insert
values
$mtgid = @mysql_insert_id(); //create variable for last inserted id

//check if meeting input is successful
if ($success) {
echo "Your ". date('M d, Y', strtotime($date)) . " meeting has been added
to the database. <br />Your meeting number is " .$mtgid .",
<br />keep this number handy for future reference. <br />";
} else {
echo "There was an error adding your meeting to the database, please try
again.";
}//end check if meeting successful

if (isset($_POST['upload'])){ //check if file upload request ?>

Please use the form below to upload your meeting documents.<br /><br />
<h2>Select files to upload:</h2Files names should not have any spaces,
single quotes, or slashes. These will be replaced with an underscore (_)
character in the file name.<br />

<form method="post" action="<?php $_SERVER['PHP_SELF']?>"
enctype="multipart/form-data">
<input type="hidden" name="numfiles" value="<?php echo $num_files ?>" />

<?php // show the file input fields based on($num_files).

for ($i = 1; $i <= $num_files; $i++) { //begin the for loop?>

<strong>File <?php echo $i ?>:</strong<input type="file" name="file<?php
echo $i ?>"/&nbsp;
<strong>Document Title:</strong>(Required - Max 50 characters):
<input type="text" size="30" maxlength="50" name="title<?php echo $i
?>"/><br />

<?php
} //end for loop ?>

<input type="hidden" name="mtgid" value="<?php echo $mtgid ?>" />
<input type="submit" name="upload_form" value="Upload Now!" />
</form>
<?
} else { //else if no upload requested

echo "<a href='new.php'>Add another meeting.</a>";

} //end check for upload request

}//end check if meeting added

if (isset($_POST['upload_form'])){ //if the upload form has been submitted,
then do the upload process

//upload directory.
$upload_dir = "docs/";
$mtgid= $_POST['mtgid'];
$num_files = $_POST['numfiles'];

echo "<h3>Upload results:</h3>";

//do a loop for uploading files based on ($num_files) number of files.
for ($i = 1; $i <= $num_files; $i++) {

//define file variables to hold the values.
$new_file = $_FILES['file'.$i];
$file_name = $new_file['name'];

//to remove spaces and apostrophes from file name we have to replace it
with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_name = str_replace("'", "_", $file_name);

// strip file_name of slashes
$file_name = stripslashes($file_name);

$file_tmp = $new_file['tmp_name'];

//define other variables
$title = $_POST['title'.$i];

//check if file selected
if (!is_uploaded_file($file_tmp)) {

//print error message and file number.
echo "File $i: Not selected.<br>";

}else{

//check if file is Already EXISTS.

if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already exists.<br>";
}else{

// upload the files.

if (move_uploaded_file($file_tmp,$upload_dir.$file_na me)) {

//enter file data into database
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meeting_docs VALUES ('', '$file_name',
'$title', '$mtgid')";
$success = mysql_query($doc_insert); //create variable to hold insert
values

if ($success) { //confirm if values added to database
echo "File $i ($file_name) has been uploaded and added to the meeting
docs table.<br>";
echo "File $i: Title: $title<br>";
echo "File $i: Meeting ID: $mtgid<br><br>";
} else {
echo "There was an error adding your meeting document to the database,
please try again.";
}//end check if meeting successful

}else{
echo "File $i: ($file_name) failed to upload.<br>";
}//end of (move_uploaded_file).

}//end of (file_exists).

}//end of (!is_uploaded_file).

}//end of (for loop).

} //end upload process

?>
Chris
Aug 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.