Multiple File Uploads..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
po
Forum Newbie
Posts: 4
Joined: Sun Oct 23, 2005 9:24 am
Location: Victoria, BC, Canada

Multiple File Uploads..

Post by po »

I don't want to be a bother, but I did a search on the subject, but nothing came up that could help me, so...

I have a page 'upload.php' which lets you select multiple files to upload to a directory of your choice (using a drop-down box) which goes to 'uploads2' so the php can do its thing.

Heres the form on the first page (10 fields):

Code: Select all

<form action="upload2.php" enctype="multipart/form-data" method="post">

<table align="center" cellpadding="0" cellspacing="0">

	<tr><td align="left"><input type="file" name="file[]"></td><td align="left"><input type="file" name="file[]"></td></tr>
	<tr><td align="left"><input type="file" name="file[]"></td><td align="left"><input type="file" name="file[]"></td></tr>
	<tr><td align="left"><input type="file" name="file[]"></td><td align="left"><input type="file" name="file[]"></td></tr>
	<tr><td align="left"><input type="file" name="file[]"></td><td align="left"><input type="file" name="file[]"></td></tr>
	<tr><td align="left"><input type="file" name="file[]"></td><td align="left"><input type="file" name="file[]"></td></tr>
	<tr><td align="center" colspan="2"><p class="text2">Choose a directory for these files to be placed in: <select name="directory"><option value="a">/<option value="b" selected>/images/<option value="c">/files/</select></p>
	<tr><td align="center" colspan="2"><input type="submit" value="Upload"></td></tr>

</table>

</form>
And here's the PHP on the second page:

Code: Select all

$directory=$_POST['directory'];

if($directory=='a')$dir='http://www.paulleduc.com/';
if($directory=='b')$dir='http://www.paulleduc.com/images/';
if($directory=='c')$dir='http://www.paulleduc.com/files/';

foreach ($_FILES[$_POST['file']]["error"] as $key => $error) {

	if ($error == UPLOAD_ERR_OK) {

	$tmp_name = $_FILES[$_POST['file']]["tmp_name"][$key];
	$name = $_FILES[$_POST['file']]["name"][$key];

	if($directory=='a')move_uploaded_file($tmp_name, "/htdocs/$name");
	if($directory=='b')move_uploaded_file($tmp_name, "/htdocs/images/$name");
	if($directory=='c')move_uploaded_file($tmp_name, "/htdocs/files/$name");	

	}	

}
I have tried changing the directory in the move_uploaded_file functions to a ton of things, like full unix path, full url, with and without the leading slashes, etc. I know that the array is working and all that because it shows the right info when I have print_r($_FILES); in there. It isn't working though, I don't get any errors, the file isn't appearing in the directory, so I can only assume that's it, but I've tried so many different directory variations. Any help would be nice.

-Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST['file'] will not exist, $_FILES['file'] will.
User avatar
po
Forum Newbie
Posts: 4
Joined: Sun Oct 23, 2005 9:24 am
Location: Victoria, BC, Canada

Post by po »

Ah, thats fixed it, thanks.
Post Reply