Skip to content
Advertisement

How to run 2 or more input files in the same form with PHP

I would like to know how to run 2 or more input files in the same form, I have to upload some documents by using php, I made separate forms and they work, but I need to all together however I dont know how. I need to put only two forms as example actually I need to put 3 but the 3rd is larger so it would be much code to read with an example putting only two I would be able to do the rest.

Note: Form 1 and Form to upload data to different tables.

Form 1

 <div class="container">
<?php
if(isset($_POST['uploadBtn'])){
    $fileName=$_FILES['myFile']['name'];
    $fileTmpName=$_FILES['myFile']['tmp_name'];
   
    $fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);

    $allowedType = array('csv');
    if(!in_array($fileExtension,$allowedType)){?>

        <div class="alert alert-danger">
            INVALID FILE
        </div>
    <?php }else{
        $handle = fopen($fileTmpName, 'r');
        $k = 0;
        $energies = array ();
        while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
          $k++;
          if ( $k > 1 ) {
                $energies[] = $myData[3];
             }
            }

            list($e1, $e2, $e3) = $energies;
            $query = "INSERT INTO metlab.resultados_impacto_junta (energy1, energy2, energy3) VALUES ($e1, $e2, $e3)";

            $run = mysql_query($query);

        if(!$run){
            die("error in uploading file".mysql_error());
        }else{ ?>
                <div class="alert alert-success">
                    SUCCESS
                </div>
    <?php   }
    }
}
    ?>

<form action="" method="post" enctype="multipart/form-data">
    <h3 class="text-center">
        RESULTS
    </h3></hr>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="file" name="myFile" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="submit" name ="uploadBtn" class="btn btn-info">
            </div>
        </div>
    </div>
</form>

Form 2

<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
    $fileName=$_FILES['myFile']['name'];
    $fileTmpName=$_FILES['myFile']['tmp_name'];
    //RUTA DEL ARCHIVO
    $fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
    //FORMATOS DE ARCHIVO PERMITIDOS
    $allowedType = array('csv');
    if(!in_array($fileExtension,$allowedType)){?>

        <div class="alert alert-danger">
            INVALID FILE
        </div>
    <?php }else{

        $handle = fopen($fileTmpName, 'r');
        $k = 0;
        while (($myData = fgetcsv($handle,1000,','))!== FALSE){
         $k++;
          if ( $k > 4 ) {

               
                $valor_dureza = $myData[3];
                
                

                $query = "INSERT INTO metlab.resultados_tension_junta (size,yield,tensile,ra,elongacion)
                VALUES ('".$valor_dureza."')";
                $run = mysql_query($query);
             }

        }
        if(!$run){
            die("error in uploading file".mysql_error());
        }else{ ?>
                <div class="alert alert-success">
                    SUCCESS
                </div>
    <?php   }
    }
}
    ?>

<form action="" method="post" enctype="multipart/form-data">
    <h3 class="text-center">
        RESULTS
    </h3></hr>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="file" name="myFile" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="submit" name ="uploadBtn" class="btn btn-info">
            </div>
        </div>
    </div>
</form>

I would like a form like this: example

With the Fk I would know which number the 3 docs belong to.

Advertisement

Answer

I solved the problem myself I just changed the vars of the forms and thats all, Its a dirty and bad solution but it works for now.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement