Page 1 of 1
Multifile upload question - almost there
Posted: Tue Nov 15, 2005 11:08 am
by psurrena
The code listed below only uploads one file. It seems to ignore 'userfileb'
Any thoughts?
Code: Select all
<?php
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileNameb = $_FILES['userfileb']['nameb'];
$tmpName = $_FILES['userfileb']['tmp_name'];
$fileSizeb = $_FILES['userfileb']['sizeb'];
$fileTypeb = $_FILES['userfileb']['typeb'];
$icon = $_POST['icon'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$adtitle = $_POST['adtitle'];
$adurl = $_POST['adurl'];
$adcopy = $_POST['adcopy'];
$city = $_POST['city'];
$state = $_POST['state'];
$bname = $_POST['bname'];
$bweb = $_POST['bweb'];
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';
$query = "INSERT INTO upload (name, size, type, content, nameb, sizeb, typeb, contentb, icon, fname, lname, email, adtitle, adurl, adcopy, city, state, bname, bweb, date) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileNameb', '$fileSizeb', '$fileTypeb', '$contentb', '$icon', '$fname', '$lname', '$email', '$adtitle', '$adurl', '$adcopy', '$city', '$state', '$bname', '$bweb', current_date)";
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
header('Location: step2_text.html');
}
?>
Posted: Tue Nov 15, 2005 11:16 am
by JAM
Code: Select all
$_FILES['userfileb']['nameb']; // bad
$_FILES['userfileb']['name']; // good
The 'nameb' part should still be just 'name'. Same goes for the other *b's.
To easier spot it, add this somewhere:
Code: Select all
echo '<pre>';
print_r($_FILES);
echo '</pre>';
Posted: Tue Nov 15, 2005 11:20 am
by psurrena
even though they are two seperate uploads going to different cells?
Code: Select all
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" class="box" id="userfile"></td>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfileb" type="file" class="box" id="userfileb"></td>
Posted: Tue Nov 15, 2005 11:23 am
by JAM
Yah, because you name your 'cells' as 'userfile' & 'userfileb'. Thats whats being used as names later in $_FILES to seperate them from each other. The 'name', 'size' etc are predefined values that doesn't need changing. Example of the output:
Code: Select all
Array
(
[userfile] => Array
(
[name] => CA5HLT52.htm
[type] => text/html
[tmp_name] => .\phpE.tmp
[error] => 0
[size] => 105
)
[userfileb] => Array
(
[name] => CA5J9HBN.html
[type] => text/html
[tmp_name] => .\phpF.tmp
[error] => 0
[size] => 1005
)
)
Posted: Tue Nov 15, 2005 11:36 am
by psurrena
My cells include the items that are named "....b"
--
id int(11) No auto_increment
name varchar(30) No
type varchar(30) No
size int(11) No 0
content blob BINARY No
nameb varchar(30) No
typeb varchar(30) No
sizeb int(11) No 0
contentb mediumblob BINARY No
fname varchar(40) No
lname varchar(40) No
email varchar(40) No
adtitle varchar(60) No
adurl varchar(100) No
adcopy varchar(255) No
bname varchar(40) No
bweb varchar(40) No
city varchar(50) No
state char(2) No 0
icon char(2) No
date No 0000-00-00
Posted: Tue Nov 15, 2005 11:43 am
by Burrito
those are the "fields" on your database. What JAM is referring to are the "fields" on your form.
when you submit your form, assuming you have the correct enctype, your fields that are "file" fields will pass along the information for the file to the server and php can then handle them.
It handles them by putting information about the files into the $_FILES[] array. If you do what JAM suggested and use print_r(), it will display all of the information in the multidimensional array. See JAM's sample output above.
Posted: Tue Nov 15, 2005 12:38 pm
by psurrena
This is what I get:
Array
(
[userfile] => Array
(
[name] => Huey Newton.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpPckdYA
[error] => 0
[size] => 34591
)
[userfileb] => Array
(
[name] => 195.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpplEEUA
[error] => 0
[size] => 30269
)
)
Posted: Tue Nov 15, 2005 12:41 pm
by psurrena
"userfileb" appears in the database, "userfile" is not there. How can I specify where I want it to be placed?
Posted: Tue Nov 15, 2005 12:42 pm
by John Cartwright
JAM wrote:Code: Select all
$_FILES['userfileb']['nameb']; // bad
$_FILES['userfileb']['name']; // good
The 'nameb' part should still be just 'name'. Same goes for the other *b's.
To easier spot it, add this somewhere:
Code: Select all
echo '<pre>';
print_r($_FILES);
echo '</pre>';
I suggest you re-read this post, as it answers your question.
Posted: Tue Nov 15, 2005 1:09 pm
by psurrena
It shows that everything should be ok:
Array
(
[userfile] => Array
(
[name] => avatar.gif
[type] => image/gif
[tmp_name] => /tmp/phpib6Q3p
[error] => 0
[size] => 2835
)
[userfileb] => Array
(
[name] => banjos.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpooLgtc
[error] => 0
[size] => 89052
)
)
BUT...now nothing is placed in the database. Am I right when I duplicated the "values" even though the names are the same?
Code: Select all
<?php
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileName = $_FILES['userfileb']['nameb'];
$tmpName = $_FILES['userfileb']['tmp_name'];
$fileSize = $_FILES['userfileb']['sizeb'];
$fileType = $_FILES['userfileb']['typeb'];
$icon = $_POST['icon'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$adtitle = $_POST['adtitle'];
$adurl = $_POST['adurl'];
$adcopy = $_POST['adcopy'];
$city = $_POST['city'];
$state = $_POST['state'];
$bname = $_POST['bname'];
$bweb = $_POST['bweb'];
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';
$query = "INSERT INTO upload (name, size, type, content, nameb, sizeb, typeb, contentb, icon, fname, lname, email, adtitle, adurl, adcopy, city, state, bname, bweb, date) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileName', '$fileSize', '$fileType', '$content', '$icon', '$fname', '$lname', '$email', '$adtitle', '$adurl', '$adcopy', '$city', '$state', '$bname', '$bweb', current_date)";
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
echo '<pre>';
print_r($_FILES);
echo '</pre>';
// header('Location: step2_text.html');
}
?>
Posted: Tue Nov 15, 2005 1:18 pm
by twigletmac
psurrena wrote:BUT...now nothing is placed in the database. Am I right when I duplicated the "values" even though the names are the same?
No, because the first lot of variables will be overwritten by the second lot. This bit should be fine:
Code: Select all
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
But this bit:
Code: Select all
$fileName = $_FILES['userfileb']['nameb'];
$tmpName = $_FILES['userfileb']['tmp_name'];
$fileSize = $_FILES['userfileb']['sizeb'];
$fileType = $_FILES['userfileb']['typeb'];
needs you to firstly change things like 'nameb' to 'name' (as has been explained previously) but you also need to give the variables different names like you had before, i.e. $fileNameb instead of $fileName.
Mac
Posted: Tue Nov 15, 2005 3:07 pm
by psurrena
sorry if this is getting annoying but if both these exist for example:
Code: Select all
$fileName = $_FILES['userfile']['name'
$fileNameb = $_FILES['userfileb']['name'];
They will try to go to the same area of the database which won't work....
Posted: Tue Nov 15, 2005 3:24 pm
by trukfixer
Geesh.. you arent getting it are you??
Your whole problem is here:
Code: Select all
$fileName = $_FILES['userfile']['name']; //OK however note this point in comment below
$tmpName = $_FILES['userfile']['tmp_name'];//ok
$fileSize = $_FILES['userfile']['size'];//ok
$fileType = $_FILES['userfile']['type'];//ok
$fileName = $_FILES['userfileb']['nameb'];// wrong - first use $_FILES['userfileb'['name'], and *SECONDLY* and most importantly - you are overwriting the *FIRST $fileName defined *ABOVE* with this value.. , therefore, your whole code is b0rked. .. also note the same variable being used in two places in your database query - same data being inserted twice..
$tmpName = $_FILES['userfileb']['tmp_name'];//see above comment
$fileSize = $_FILES['userfileb']['sizeb'];//see above comment
$fileType = $_FILES['userfileb']['typeb'];//see above comment
$icon = $_POST['icon'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$adtitle = $_POST['adtitle'];
$adurl = $_POST['adurl'];
$adcopy = $_POST['adcopy'];
$city = $_POST['city'];
$state = $_POST['state'];
$bname = $_POST['bname'];
$bweb = $_POST['bweb'];
//note in the query below two identical $fileName values inserted, that means you are inserting the same value in two places, I doubt thats what you want.
$query = "INSERT INTO upload (name, size, type, content, nameb, sizeb, typeb, contentb, icon, fname, lname, email, adtitle, adurl, adcopy, city, state, bname, bweb, date) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$fileName', '$fileSize', '$fileType', '$content', '$icon', '$fname', '$lname', '$email', '$adtitle', '$adurl', '$adcopy', '$city', '$state', '$bname', '$bweb', current_date)";
heads up: variables are being overwritten in your code, in addition to incorrect usage /assignment of $_FILES fields.