php segmented file uploads seperated in array - php

I have created an dynamic registration table - and related to one of the segments I need to be able to upload a file.
It all works like dandy. Except the fileupload.
Doing an print_r($_FILES); after attempted upload gives this:
[0]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(16) "image001 (3).jpg"
[1]=>
string(16) "image001 (2).jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpdqCWSl"
[1]=>
string(14) "/tmp/phpabqqwj"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(1288)
[1]=>
int(1288)
}
}
}
Here it kind of merges the two uploads into the same array instead of making the array size of two sub arrays.
<td style='display:none;' class=td_register_purchases>
<input type=text name=registration[][purchase_data] id=purchase_data class=purchase_data>
<select name='registration[][purchase_type]' id=select_purchase class=purchase_type>
<option value=1>Accomodation</option>
<option value=2>Catering</option>
<option value=3>Tools</option>
<option value=4>Software/License</option>
<option value=5>Hardware</option>
</select>
<textarea rows=2 cols=50 name='registration[][purchase_comment]' class=purchase_comment placeholder='Comment Area'></textarea>
<input type=file name='upload_file[]' class='upload-file'> Attachment
</td>
I could work with this result:
[0]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(16) "image001 (3).jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpdqCWSl"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(1288)
}
}
[1]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(16) "image001 (2).jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpabqqwj"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(1288)
}
}
}
But I am probably making a mistake somewhere that could fix it all.

Working hard.. And found a - not beautifull solution ;-)
$number_files = sizeof($_FILES["upload_file"]["name"]);
$fields = 4;
$field_count = 0;
$row_count = 0;
if (!empty($_FILES)) {
for($x = 0; $x < $number_files; $x++) {
foreach ($_FILES AS $reg_array) {
foreach ($reg_array AS $segment => $value)
{
if ($field_count == $fields)
{
$temp_files[$row_count][$segment] = $value[$x];
$field_count = 0;
$row_count++;
}
else
{
$field_count++;
$temp_files[$row_count][$segment] = $value[$x];
}
} // end foreach
} // end foreach
} // end for
} // end if

Related

Insert PHP Regex Exploded Array Into MySQL Table

I have an array that I exploded by Regex, here is a part of the array named $data;
array(6) {
[0]=>
array(1) {
[0]=>
string(77) "Achnanthes brevipes C.Agardh, Syst. Alg.: 1 (1824). / Küçük sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
[3]=>
array(1) {
[0]=>
string(8) "C.Agardh"
}
[4]=>
array(1) {
[0]=>
string(21) "Syst. Alg.: 1 (1824)."
}
[5]=>
array(1) {
[0]=>
string(22) "Küçük sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(89) "Achnanthes cocconeiformis Mann, U.S. Nat. Mus., Bull. 6: 182 (1925). / Top sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(14) "cocconeiformis"
}
[3]=>
array(1) {
[0]=>
string(4) "Mann"
}
[4]=>
array(1) {
[0]=>
string(36) "U.S. Nat. Mus., Bull. 6: 182 (1925)."
}
[5]=>
array(1) {
[0]=>
string(17) "Top sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(108) "Achnanthes gibberula Grunow, Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880). / Kambur sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(9) "gibberula"
}
[3]=>
array(1) {
[0]=>
string(6) "Grunow"
}
[4]=>
array(1) {
[0]=>
string(55) "Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880)."
}
[5]=>
array(1) {
[0]=>
string(20) "Kambur sucıncığı"
}
}
and many more..
my regex is:
$turRegex = '/^([^\s]+)[\s]([^\s]+)[\s]([^\s]+)[,][\s]([A-Za-z].+)[\s][\/][\s](.+)[.]/m';
my foreach loop:
foreach ($data as $data) {
if (!empty(preg_match_all($turRegex, $data, $matches))) {
echo "<pre>";
var_dump($matches);
echo "</pre>";
}
}
My table name is "algeaSpecies" and columns are; "id","genusName","speciesEpiteth","author", "publication","TurkishName"
I want to insert this to genusName;
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
this to speciesEpiteth;
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
and others...
I'm using MySQLi
Thank you
First I noticed you used $data both as array and item in your loop.
In the following code, I assumed that you don't want the first index (the query wasn't clear).
Also, replace the DB connection credentials.
$con = new mysqli('db_host', 'username', 'password', 'db_name');
$boundArray = array_fill(0, 5, null);
$query = "INSERT INTO `algeaSpecies` (`genusName`, `speciesEpiteth`, `author`, `publication`, `TurkishName`) VALUES (?,?,?,?,?);";
$stmt = $con->prepare($query);
$stmt->bind_param("sssss", ...$boundArray);
foreach ($data as $item) {
$i = -1;
foreach ($item as $index) {
if ($i === -1) {
$i++;
continue;
}
$boundArray[$i++] = $index[0];
}
$stmt->execute();
}
$stmt->close();
$con->close();

how to get the index of every array on multidimensional array with unknown depth in php

array(2) { [0]=> array(2) { ["name"]=> string(16) "Daerah Pertanian" ["sub"]=> array(6) { [0]=> array(2) { ["name"]=> string(5) "Sawah" ["value"]=> string(3) "145" } [1]=> array(2) { ["name"]=> string(18) "Sawah Pasang Surut" ["value"]=> string(3) "455" } [2]=> array(2) { ["name"]=> string(6) "Ladang" ["value"]=> string(3) "678" } [3]=> array(2) { ["name"]=> string(10) "Perkebunan" ["value"]=> string(3) "688" } [4]=> array(2) { ["name"]=> string(19) "Perkebunan Campuran" ["value"]=> string(3) "966" } [5]=> array(2) { ["name"]=> string(16) "Tanaman Campuran" ["value"]=> string(3) "565" } } } [1]=> array(2) { ["name"]=> string(22) "Daerah Bukan Pertanian" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(18) "Hutan Lahan Kering" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(25) "Hutan Lahan Kering Primer" ["value"]=> string(3) "566" } [1]=> array(2) { ["name"]=> string(27) "Hutan Lahan Kering Sekunder" ["value"]=> string(3) "255" } } } [1]=> array(2) { ["name"]=> string(17) "Hutan Lahan Basah" ["sub"]=> array(2) { [0]=> array(1) { ["name"]=> string(24) "Hutan Lahan Basah Primer" } [1]=> array(1) { ["name"]=> string(26) "Hutan Lahan Basah Sekunder" } } } } } }
I have an array like I mention above, so I want to print out every "name" key including the index (number) of it's array parent,
for example when I print out "Tanaman Campuran" so all index parent is (0)(5) and when I print "Hutan Lahan Basah Sekunder" the index parent is (1)(1)(1)
how can I achieve it?
here is some recursive function that I've tried
$GLOBALS['all'] = '';
function printout($arr){
foreach ($arr as $ia=>$a){
if(is_array($a)){
foreach ($a as $ib=>$b){
if(is_array($b)){
printout($b);
}
else{
if ($ib == 'name') {
$GLOBALS['all'] .= $ia;
echo '<tr>';
echo '<td>' . $b . ' (' . $ia . ')</td>';
echo '</tr>';
$GLOBALS['all'] = '';
}
}
}
}
}
}
*sorry for my bad explanation, I hope you guys can understand it
You could use the following function:
function search(array $array, $name)
{
foreach ($array as $key => $entry) {
if ($entry['name'] === $name) {
return [$key];
}
if (isset($entry['sub']) && $found_keys = search($entry['sub'], $name)) {
return array_merge([$key], $found_keys);
}
}
return null;
}
It returns:
if the value was directly found, an array of one containing the associated index,
if it wasn't but was found in any descendant item, an array merging its index with the indices of said descendant,
null if it wasn't found in that part of the tree.
Note: if a given name is present several times, it will only find the first occurrence.
Demo: https://3v4l.org/1hGr1

Removing the key of associative array not working

I have an associative array like the following.
array(1) {
["ftr_file_uploads"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
array(1) {
[0]=>
string(13) "Hydrangeas.jpg"
}
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
}
[1]=>
array(2) {
["name"]=>
array(1) {
[0]=>
string(13) "w.jpg"
}
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
}
}
}
I need to change the array structure to the following format.
array(1) {
["ftr_file_uploads"]=>
array(2) {
["name"]=>
array(2) {
[0]=>
string(14) "Hydrangeas.jpg"
[1]=>
string(5) "w.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
}
}
I have tried the following.
foreach($_FILES['ftr_file_uploads'] as $keyval1=>$value1) {
$_FILES['ftr_file_uploads'][] = $_FILES[$keyval1];
unset($_FILES[$keyval1]);
}
Sample code:
$a=array('ftr_file_uploads'=>[['name'=>['Hyd.jpg']],['name'=>['w.jpg']] ]);
$arr = array();
foreach($a['ftr_file_uploads'] as $files){
$arr['file_upload']['name'][]=$files['name'][0];
}
print_r($arr);
I have done the following to obtain the result
foreach($_FILES['ftr_file_uploads'] as $key=>$files){
foreach(array_keys($files) as $k=>$v)
{
$newarray['ftr_file_uploads'][$v][]=$files[$v][0];
}
}

Issue with uploading file inputs with multiple names

I have several file input fields with a common name and a unique name for each file input .The unique name is for some validation purposes.
<input name="file12 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
...............
When trying to upload files in PHP form submit,
the content of $_FILES is as following.
array(2) {
["file1_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(13) "Jellyfish.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpx7iId2" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(775702) } }
["file2_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(12) "Penguins.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpN6QWoD" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(777835) } }
}
The array key name changed to the concatenated names of the file input field.I need the names to be ftr_file_uploads rather than fileIDnumber_ftr_file_uploads.
I have done like the following.
foreach($_FILES as $keyval=>$value)
{
$_FILES['ftr_file_uploads'] = $_FILES[$keyval]; //removed
$_FILES['ftr_file_uploads'][] mentioned in the answer
unset($_FILES[$keyval]);
}
When i am using like this,I am getting the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(14) "Lighthouse.jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpLdslxb"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(561276)
}
}
}
I need the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(14) "Hydrangeas.jpg"
[1]=>
string(5) "w.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpKMwmH1"
[1]=>
string(14) "/tmp/phpwwHU9G"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(595284)
[1]=>
int(879394)
}
}
}
foreach ($_FILES as $name => $file) {
$_FILES['ftr_file_uploads'][] = $file; // [] means add $file to $_FILES['ftr_file_uploads'] array
unset($_FILES[$name]); // remove element from $_FILES
}

Get filename of file which is about to upload - PHP

I want to get the filename of a file I´m about to upload and I´m trying it with this code:
$filename = $_FILES['file']['name'];
But sadly I get back "Array". Can someone please explain me why?
array(1) {
["file"]=> array(5) {
["name"]=> array(1) {
[0]=> string(12) "download.jpg"
}
["type"]=> array(1) {
[0]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(1) {
[0]=> string(38) "/home/.sites/60/site1048/tmp/phpj38Yhn"
}
["error"]=> array(1) {
[0]=> int(0)
}
["size"]=> array(1) {
[0]=> int(12494)
}
}
}
$filename = $_FILES['file']['name'][0];

Categories