Sol Sol - 4 months ago 12
PHP Question

How do you post and get unique names of the post names?

I was trying to catch the names of this array and then I can sanitize it afterward. I am just not sure how to use $_REQUEST to catch each 'files(#number)' name that comes through.

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="upload.php">
File: <input type="file" name="files1" multiple="multiple"/><br/>
File: <input type="file" name="files2" multiple="multiple"/><br/>
File: <input type="file" name="files3" multiple="multiple"/><br/>
File: <input type="file" name="files4" multiple="multiple"/><br/>
<input type="submit">
</form>
</body>
</html>


upload.php

if ($_REQUEST) {
// my problem is right here to catch the numbers of 'files'
foreach ($_REQUEST['files'] as $key => $value) {
echo $key . '-->' . $value . '<br/>';
}
} else {
echo 'error';
}



print '<pre>';
print_r($_REQUEST);
print '</pre>';


Output because of print_r($_REQUEST) :

Array
(
[files1] => focus48.png
[files2] => focus48.png
[files3] => focus48.png
[files4] => focus48.png
)

DDJ DDJ
Answer

With each file input in this form you can have a hidden input that has your number as an array. You don't need the id=number for the file input, but I left them for your reference.

    <!DOCTYPE html>
    <html>
        <head>
          <title></title>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
       <body>
          <form action="upload.php">
             File: <input type="file" name="files[]" id="1" multiple="multiple"/><br/>
            <input name='file_id' value='1' type='hidden' />

             File: <input type="file" name="files[]" id="2"  multiple="multiple"/><br/>
            <input name='file_id' value='2' type='hidden' />

             File: <input type="file" name="files[]" id="3"  multiple="multiple"/><br/>
            <input name='file_id' value='3' type='hidden' />

             File: <input type="file" name="files[]" id="4"  multiple="multiple"/><br/>
            <input name='file_id' value='4' type='hidden' />

             <input type="submit">
          </form>
       </body>
    </html>

Here is how you get the numbers with a hidden array of numbers (arrays start with 0):

            if ($_REQUEST) {
       foreach ($_REQUEST['files'] as $key => $value) {
          echo $key . '-->' . $value . '<br/>';
       }
    } else {
       echo 'error';
    }



    print '<pre>';
    print_r($_REQUEST);
    print '</pre>';

     $file_id = $_POST['file_id'];
     echo $file_id;

Output:

0-->file.txt
1-->file.txt
2-->file.txt
3-->file.txt

Array
(
    [files] => Array
        (
            [0] => file.txt
            [1] => file.txt
            [2] => file.txt
            [3] => file.txt
        )

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

)