You are here: Re: uploading several docs at once and inputting data into a db « PHP Programming Language « IT news, forums, messages
Re: uploading several docs at once and inputting data into a db

Posted by Chris on 08/08/06 22:29

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)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("Error: The directory <b>($upload_dir)</b> is 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_name)) {
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" <tim.n.hunt@gmail.com> wrote in message
news:1154999040.494562.207830@p79g2000cwp.googlegroups.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
>

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация