How to upload several files with different inputs?

0

I wish that you can choose only one file for each input and upload them all and apart from inserting certain data, I have the following code:

<?php
    session_start();

    $folders    = $conn->query("SELECT * FROM 'folders' ORDER BY 'id' ASC");
    $tfolders   = $folders->num_rows;

    $users      = $conn->query("SELECT * FROM 'users' ORDER BY 'id' ASC");

    if (isset($_POST['sfile'])) {
        while($row=$folders->fetch_array()) { 
                $target_dir     = "files/".$_SESSION['zona']."/";
                $extension      = '.' . pathinfo($_FILES[$row['id']]["name"],PATHINFO_EXTENSION);

                if (move_uploaded_file($_FILES[$row['id']]["tmp_name"], "files/". $_SESSION['zona'] . "/" . $row['name'] . $extension)) {
                    $stmt = $conn->prepare("INSERT INTO 'upfiles' ('name', 'extension', 'folderid', 'zona') VALUES (?, ?, ?, ?)") or trigger_error($conn->error);
                    $stmt->bind_param("ssis", $name, $extension, $folderid, $zona);

                    $name       = $row['name'];
                    $folderid   = $row['id'];
                    $zona       = $_SESSION['zona'];

                    $stmt->execute();
                    $stmt->close();
                }
                $func->redirect('index.php');
        }   
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title>Prueba</title>
</head>
<body>
<main style="text-align: center;">
<?php
    echo "<form method='POST' enctype='multipart/form-data'>";

    while($row=$folders->fetch_array())
    {
        $id = $row['id'];
        $name = $row['name'];
        echo "<input type='file' name='".$id."' id='selectedFile".$id."' style='display: none;' />";
        echo "<input type='hidden' name='fid".$id."' value='".$id."'/>";
        echo '<div id="button'.rand(1,10).'" class="boton" onclick="document.getElementById(\'selectedFile'.$id.'\').click();">'.$name.'</div>';
    }
    echo "<br><button type='submit' name='sfile'>Enviar archivos</button></form>";
?>

</main>

</body>
</html>

The problem here is that if I select all the files in each of the inputs, only the first one is uploaded, some ideas?

    
asked by Alejandro Montoya 09.04.2017 в 07:44
source

1 answer

0

The problem is that you send several files as if they were 1. When what you send is an array of different files.

What you should do is send a file arrangement to get 1 to 1.

<input name="imagenes[]" type="file" /><br />

<input name="imagenes[]" type="file" /><br />

In php

$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);

check this php information

    
answered by 09.04.2017 / 08:05
source