Forum Moderators: coopster

Message Too Old, No Replies

Need help with count function

         

lazarus

10:21 am on Oct 8, 2003 (gmt 0)

10+ Year Member



Hi all, I'm trying to get my script to count the number of files that a user wants to upload, but i can't get the count function to count them. Here is a code snippet.

<form action="<? echo $PHP_SELF;?>" method="POST" ENCTYPE="multipart/form-data">
<tr>
<td valign="top"><b><font size="-1">Title</font></b></td>
<td><input size="53" maxlength="250" type="text" name="title"></td>
</tr>
<tr>
<td valign="top"><b><font size="-1">Taster</font></b></td>
<td><textarea name="taster" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<td valign="top"><b><font size="-1">Story</font></b></td>
<td><textarea name="full" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td valign="top"><font size="-1">Image 1</font></td>
<td><input size="53" name="userfile[]" type="file"></td>
</tr>
<tr>
<td valign="top"><font size="-1">Image 2</font></td>
<td><input size="53" name="userfile[]" type="file"></td>
</tr>
<tr>
<td valign="top"><font size="-1">Image 3</font></td>
<td><input size="53" name="userfile[]" type="file"></td>
</tr>
<tr>
<td valign="top"><font size="-1">Image 4</font></td>
<td><input size="53" name="userfile[]" type="file"></td>
</tr>
<tr>
<td colspan=2><input type="Submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
<?

}
else
{
// includes
include("../conf.php");
include("../functions.php");

// set up error list array
$errorList = array();
$count = 0;

// validate text input fields
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }

if (!$taster) { $errorList[$count] = "Invalid entry: Taster"; $count++; }

if (!$full) { $errorList[$count] = "Invalid entry: Story"; $count++; }

// check for errors
// if none found...
if (sizeof($errorList) == 0)
{

// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

//generate and execute query

$query1 = "INSERT INTO story(story_title, story_taster, story_full, timestamp) VALUES('$title', '$taster', '$full', NOW())";
$result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error());
$dirname = mysql_insert_id();
@mkdir("images/$dirname", 0777);

$story_id = mysql_insert_id();

//*******************************
//* Place the loop script here *
//*******************************

$tot = count($userfile);
for($i=0;$i>$tot;$i++)
{

if (is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
{
$filename = $_FILES['userfile']['name'][$i];
copy($_FILES['userfile']['tmp_name'][$i], "images/$dirname/$filename");
$secondquery = "INSERT INTO images(story_id, images_name) VALUES ('$story_id', '$filename')";
$secondresult = mysql_query($secondquery) or die(mysql_error());
}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}

// print result
echo "<font size=-1>Update successful.</font>";
echo "<a href=list.php>Go back to the main menu</a>";

// close database connection
mysql_close($connection);

}
}

Any idea's?

ergophobe

3:59 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is happening?

I always use the $_FILES[['blah']['blah'], but $userfile should be the same as $_FILES['userfile']['tmp_name']

So count($userfile) should be returning 4 in all cases, no matter how many files are uploaded, because the array elements are there, it's just that they are empty.

What you need to do is test and increment like so

$tot = 0;
while ($HTTP_POST_FILES['userfile']['name'][$tot]) {
echo $HTTP_POST_FILES['userfile']['name'][$tot] . "<br>";
$tot++;
}

That way you stop counting when you hit the empty arrays (i.e. user has not filled in that box).

Tom

lazarus

10:54 am on Oct 9, 2003 (gmt 0)

10+ Year Member



you are a star, thank you.