How to index and extract array values inside a PHP for loop? Is there a way to do it?
I have included var_dump output and asked my question inside the following PHP upload script in comment out section.
My upload script is as following:
$updir = "/public_html/";
if (isset($_FILES['upload'])) {
for($f=0; $f<count($_FILES['upload']['name']); $f++) {
$file = $_FILES['upload']['name'][$f];
$file_name = pathinfo($file, PATHINFO_FILENAME);
$new_file_name = basename($file_name.'.'.'pdf');
$path = ($updir . $new_file_name);
if (move_uploaded_file ($_FILES['file_up']['tmp_name'][$f], $path)) {
echo "Success";
var_dump($path);
// $path output here is as following:
// string(23) "/public_html/image1.pdf"
// string(23) "/public_html/image2.pdf"
//
$dir = array($path);
var_dump($dir);
// $dir output here is as following:
//
//
// array(1) {
// [0]=> string(23) "/public_html/image1.pdf"
// }
//
// array(1) {
// [0]=> string(23) "/public_html/image2.pdf"
// }
//
// HOW TO MERGE THE ABOVE TWO ARRAYS WITH AN INDEX HERE LIKE
//
// array(1) {
// [0]=>
// string(23) "/public_html/image1.pdf"
// [1]=>
// string(23) "/public_html/image2.pdf"
// }
//
// AND OUTPUT THE ARRAY KEY VALUES INDIVIDUALLY?
}
}
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" class="file_up" name="file_up[]" multiple>
<input type="submit" value="UPLOAD" id="submit" />
</form>
The example current $_FILES array for two uploaded files are as following:
array(1) {
["upload"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(10) "image1.pdf"
[1]=>
string(10) "image2.pdf"
}
["type"]=>
array(2) {
[0]=>
string(9) "image/pdf"
[1]=>
string(9) "image/pdf"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phprHDGH2"
[1]=>
string(14) "/tmp/phpHGGFjH"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(46556)
[1]=>
int(37747)
}
}
}