Help in file upload

hi to everyone and sorry for my English. in a html form, a user can upload from one to four file intwo different category (A and B)

<table>
<tr>
<td><b><i>Categoria a</i><b/>
</td>
<td><b><i>Categoria b</i><b/>
</td>
</tr>
<tr>
<td style="vertical-align: top; width: 475px;"><br /><b>File 1a<b/></td>
<td style="vertical-align: top; width: 475px;"><br /><b>File 1b<b/></td>
</tr>

<tr>
<td><br>
<input name="file" type="file">
</td>
<td><br>
<input name="file1" type="file"></td>
</tr>

<tr>
<td> Luogo dello scatto *:<br>
<input name="luogo1" type="text"><br>
</td>
<td> Luogo dello scatto *:<br>
<input name="luogo2" type="text"> <br>
</td>
</tr>
<tr>
<td> Titolo  *:<br>
<input name="titolo1" type="text"><br>
</td>
<td> Titolo  *:<br>
<input name="titolo2" type="text"> <br>
</td>
</tr>


<tr>
<td style="vertical-align: top; width: 475px;"><br /><b>File 2a<b/></td>
<td style="vertical-align: top; width: 475px;"><br /><b>File 2b<b/></td>
</tr>

<tr>
<td><br>
<input name="file2" type="file">
</td>
<td><br>
<input name="file3" type="file"></td>
</tr>


<tr>
<td> Luogo  *:<br>
<input name="luogo3" type="text"><br>
</td>
<td> Luogo *:<br>
<input name="luogo4" type="text"> <br>
</td>
</tr>
<tr>
<td> Titolo *:<br>
<input name="titolo3" type="text"><br>
</td>
<td> Titolo  *:<br>
<input name="titolo4" type="text"> <br>
</td>
</tr>

they are then passed to the upload.php file

… for each file:


$file_1 = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_1_loc = $_FILES['file']['tmp_name'];
	$file_1_size = $_FILES['file']['size'];
	$file_1_type = $_FILES['file']['type'];	
	$folder1= "uploads/categoria_a/";
	
$file_2 = rand(1000,100000)."-".$_FILES['file1']['name'];
    $file_2_loc = $_FILES['file1']['tmp_name'];
	$file_2_size = $_FILES['file1']['size'];
	$file_2_type = $_FILES['file1']['type'];
	$folder2 = "uploads/categoria_b/";
	
and so on...

then the upload…

												
if (move_uploaded_file($file_1_loc,$folder1.$final_file_1) || move_uploaded_file($file_2_loc,$folder2.$final_file_2) || move_uploaded_file($file_3_loc,$folder1.$final_file_3) || move_uploaded_file($file_4_loc,$folder2.$final_file_4))
													
																					
												
													{
													$sql="INSERT INTO .. 
(here all the data entry part in the sql  table that works..... 

 ,,,                              ','$caricateil')";
													mysql_query($sql);	


										<script>
										alert("Invio dati eseguito correttamente. ");
										window.location.href='index.php?success';
										</script>
										<?php
			
													} else 	{
															?>
															<script>
															alert("Errore nell'invio dei dati");
															window.history.back(1); 
															</script>
															<?php
															}
													} 
	
									} ......

well… if I use the code

if (move_uploaded_file($file_1_loc,$folder 1.$final_file_1) || move_uploaded_file($file_2_loc,$folder 2.$final_file_2 )|| move_uploaded_file($file_3_loc,$folder 1.$final_file_3) || move_uploaded_file($file_4_loc,$folder 2.$final_file_4))
{
$sql="INSERT INTO …

Loads only the first of the 4 files it finds even if the db is populated correctly with file name size…

If, on the other hand, I use

if (move_uploaded_file($file_1_loc,$folder 1.$final_file_1) &&move_uploaded_file($file_2_loc,$folder 2.$final_file_2) && move_uploaded_file($file_3_loc,$folder 1.$final_file_3) &&move_uploaded_file($file_4_loc,$folder 2.$final_file_4))

Works… but only if all 4 files are present.

However, a person can only upload one… in position 1, 2, 3 or 4…

If one of the 4 is empty, the upload stops. I hope I explained myself…
If I load files 1, 3 and 4, load only the first…

If I insert files 2 and 4, load only the 2 etc.
And also from error even if it uploads the first files…

I would like you to upload the files and continue the script regardless of which file is up-to-date…

I hope I have been clear.

First of all, use arrays for the form fields, by using [] on the end of the field name attributes, rather than sequential numbers. This will let you operate on the submitted form data as a set, using php array functions/loops, so that you don’t need to write out blocks of code for each field. You will end up with submitted data that looks like -

Post:
Array
(
    [luogo] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [titolo] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

)
Files:
Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                )

            [type] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                )

            [error] => Array
                (
                    [0] => 4
                    [1] => 4
                    [2] => 4
                    [3] => 4
                )

            [size] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

        )

)

You will note in the example data I posted above that the [error] elements contain a 4 (UPLOAD_ERR_NO_FILE). This occurs when no file is selected. You would use this in the processing logic to ignore fields where no file was selected. The [error] element will contain a zero (UPLOAD_ERR_OK) for files that were successfully uploaded. The php.net file upload documentation contains definitions of all the possible upload error values. For errors that the user can affect, you would setup and display appropriate error messages if they occur.

Next, you would not lump together the testing and processing of the data in one statement. What are the ‘business’ rules for this? Can no (zero) files be uploaded? Must at least one file be uploaded in each category? If a file is uploaded for category A, can zero files be uploaded for category B and vice versa? … Once you have defined the business rules, you would write logic to test if the successfully uploaded files meet those rules, then only use the successfully uploaded file data.

Lastly, your post method form processing code should detect if a post method form was submitted, then detect if the $_FILES array is empty (the $_POST array will be empty as well.) Assuming no other issues in the form or on the server, this condition indicates that the total size of the submitted form data exceeds the post_max_size setting on the server. You would setup and display a message for the user telling them that the total form data was too large and cannot be processed.

the only rules is that one file must be uploaded, in category A or B. But you can upload 4 file max, 2 in every category . Then the files must be uploaded in a server subdirectory, dipend on the category where file are selected,

for every file i must insert in sql table also the title and an description (as in form code…)

Sponsor our Newsletter | Privacy Policy | Terms of Service