Page 1 of 1

Uploading Files

Posted: Thu Mar 10, 2005 6:10 am
by phulla
Maybe i am being really thick and missing something simple, but can someone please help.

I'm trying to expand the single file upload form into a multiple user defined upload files form. Where the single form allows you to upload single files, i have managed to modify the form so that the user can select how many files they want to upload in a single submit

The single file upload script i am using is as follows:

Code: Select all

<form enctype='multipart/form-data' action='admin.php?page=upload_images' method='post' name='upload_form'>
<tr>
	<td style=&quote;text-align:right;&quote;>Select Category</td>
	<td>
		<select name='category'>
			<option value=&quote;1&quote;>Jan</option>
			<option value=&quote;2&quote;>Feb</option>
			<option value=&quote;3&quote;>Mar</option>
			<option value=&quote;4&quote;>Apr</option>
			<option value=&quote;5&quote;>May</option>
		</select>
	</td>
</tr>
<tr><td><p>&nbsp;</p></td></tr>
<tr>
	<td style=&quote;text-align:right;&quote;>Photo:</td>
	<td><input name='photo_filename&#1111;]' type='file' /></td>
</tr>
<tr style=&quote;vertical-align:top;&quote;>
	<td style=&quote;text-align:right;&quote;>Caption:</td>
	<td width=&quote;100%&quote;><textarea name='photo_caption&#1111;]' cols='30' rows='1'></textarea></td>
</tr>
<tr>
	<td><input type='submit' name='submit' value='Add Photos' /></td>
</tr>
</form>
which stores an array in the form

Code: Select all

Array
(
    &#1111;name] => Array
        (
            &#1111;0] => 001.jpg
        )

    &#1111;type] => Array
        (
            &#1111;0] => image/jpeg
        )

    &#1111;tmp_name] => Array
        (
            &#1111;0] => C:\PHP\uploadtemp\phpD3B.tmp
        )

    &#1111;error] => Array
        (
            &#1111;0] => 0
        )

    &#1111;size] => Array
        (
            &#1111;0] => 118007
        )

)
which is easy to itterate and perform the necessary functions.

What i have done is modify the upload form to allow any number of uploads in one submit. So the example below allows 6 uploads.

What i can't do is manipulate the array so that i can itterate through it. The array to store 6 uploads looks like this:

Code: Select all

Array
(
    &#1111;name] => Array
        (
            &#1111;0] => 001.jpg
            &#1111;1] => 002.jpg
            &#1111;2] => 004.jpg
            &#1111;3] => 005.jpg
            &#1111;4] => 006.jpg
            &#1111;5] => 009.jpg
        )

    &#1111;type] => Array
        (
            &#1111;0] => image/jpeg
            &#1111;1] => image/jpeg
            &#1111;2] => image/jpeg
            &#1111;3] => image/jpeg
            &#1111;4] => image/jpeg
            &#1111;5] => image/jpeg
        )

    &#1111;tmp_name] => Array
        (
            &#1111;0] => C:\PHP\uploadtemp\phpD3C.tmp
            &#1111;1] => C:\PHP\uploadtemp\phpD3D.tmp
            &#1111;2] => C:\PHP\uploadtemp\phpD3E.tmp
            &#1111;3] => C:\PHP\uploadtemp\phpD3F.tmp
            &#1111;4] => C:\PHP\uploadtemp\phpD40.tmp
            &#1111;5] => C:\PHP\uploadtemp\phpD41.tmp
        )

    &#1111;error] => Array
        (
            &#1111;0] => 0
            &#1111;1] => 0
            &#1111;2] => 0
            &#1111;3] => 0
            &#1111;4] => 0
            &#1111;5] => 0
        )

    &#1111;size] => Array
        (
            &#1111;0] => 118007
            &#1111;1] => 183868
            &#1111;2] => 138343
            &#1111;3] => 128347
            &#1111;4] => 185776
            &#1111;5] => 149777
        )

)
what i have tried to do is re-build the array in the form:

Code: Select all

Array
(
    &#1111;0] => Array
        (
            &#1111;name] => 001.jpg
            &#1111;type] => image/jpeg
            &#1111;tmp_name] =>  C:\PHP\uploadtemp\phpD3C.tmp
            &#1111;error] => 0
            &#1111;size] => 118007
        )

    &#1111;1] => Array
        (
            &#1111;name] => 
            &#1111;type] => 
            &#1111;tmp_name] =>  
            &#1111;error] =>  
            &#1111;size] =>  
        )

    &#1111;2] => Array
        (
            &#1111;name] => 
            &#1111;type] => 
            &#1111;tmp_name] =>  
            &#1111;error] =>  
            &#1111;size] =>  
        )

    &#1111;3] => Array
        (
            &#1111;name] => 
            &#1111;type] => 
            &#1111;tmp_name] =>  
            &#1111;error] =>  
            &#1111;size] =>  
        )

    &#1111;4] => Array
        (
            &#1111;name] => 
            &#1111;type] => 
            &#1111;tmp_name] =>  
            &#1111;error] =>  
            &#1111;size] =>  
        )

    &#1111;5] => Array
        (
            &#1111;name] => 
            &#1111;type] => 
            &#1111;tmp_name] =>  
            &#1111;error] =>  
            &#1111;size] =>  
        )
)
but i am having difficulty with array_merge since keys are overwritten.

Can anyone provide any clues or help with either manipulating the original array so that i can extract the values for each file or re-building the array in the new format.



Thanks.

Posted: Thu Mar 10, 2005 8:09 am
by anjanesh
Why cant you iterate like this :

Code: Select all

for ($i=0;$i<count($_FILES['photo_filename']['name']);$i++)
 {
 	if ($_FILES['photo_filename']['name'][$i]!="")
 	 {
 	 	// Upload
 	 }
 }