Reputation: 17530
I'm creating a page when user can enter how many files he want to upload then that page is rendered again with a form, after submission to another page the files are uploaded.
But i'm receiving an error [if i upload 1 file, else the problem is userfile[limit-1]]
Notice: Undefined index: userfile0 in E:\wamp\www\uploader\uploader.php on line 10
I'm pasting here my code index.php
<html>
<?php
if (isset($_POST['num']) && !empty($_POST['num']))
{
?>
<form name="uploader" action="uploader.php" method="post">
<table>
<tr><td>Title</td><td>Select File</td><td>Description</td></tr>
<input type="text" name="number" value="<?php echo $_POST['num']; ?>"/>
<?php
for ($i=0;$i<$_POST['num'];$i++)
echo '<tr><td><input type="text" name="title'.$i.'"/></td>
<td><input type="file" id="userfile'.$i.'" name="userfile'.$i.'" size="30"></td>
<td><textarea name="desc'.$i.'" rows="4"></textarea></td></tr>';
?>
</table>
<input type="submit" name="b" value="Submit"/>
</form>
<?php
}
else
{
?>
<form name="form" method="post">
How many files to upload ? <input type="text" name="num" value="1"/>
<input type="submit" value="Submit"/>
</form>
<?php
}
?>
</html>
uploader.php
<?php
if (isset($_POST['number']))
{
for ($slot = 0; $slot < $_POST['number']; $slot++)
{
$title = $_POST["title$slot"];
$desc = $_POST["desc$slot"];
if (move_uploaded_file($_FILES["userfile$slot"]['tmp_name'],$_FILES["userfile$slot"]['name']))
echo $name.' file Uploaded !';
else
echo ' file not Uploaded ! ';
}
}
?>
Edit
SQL code removed
Upvotes: 0
Views: 1436
Reputation: 70460
You need an enctype="multipart/form-data"
on your <form>
tag, otherwise it cannot upload files/PHP cannot read it.
Upvotes: 2
Reputation: 5943
You should also check the existance of a file before trying to upload it.
if (isset($_FILES['userfile'.$slot])) ...
Upvotes: 1