Issue with uploading file inputs with multiple names - php

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
}

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();

php segmented file uploads seperated in array

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

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];
}
}

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];

Merge Two csv files with php

i have two csv files: one contracts data and the other containing awarded contracts. I need to combine these two files using common field(contractname) and compute total amount of current awarded contracts.
Link to csv files:
https://github.com/younginnovations/problem-statements/tree/master/clean-up-contracts-data
I do get desired output bt undefined offset error comes up.
the code:unset(awards_info[$y][0]); does unsets the data but 'Undefined offset: 0' error occurs..
similarly the code $list_data[10]!=NULL also shows the same error
but both are producing desired output.
var_dump($awards_info):: produces following:
array(5) { [0]=> array(6) { [0]=> string(12) "contractName" [1]=> string(12) "contractDate" [2]=> string(14) "completionDate" [3]=> string(7) "awardee" [4]=> string(15) "awardeeLocation" [5]=> string(6) "Amount" } [1]=> array(6) { [0]=> string(15) "Contract-2070-3" [1]=> string(8) "5/9/2014" [2]=> string(9) "8/25/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "200000" } [2]=> array(6) { [0]=> string(15) "Contract-2070-5" [1]=> string(9) "3/18/2014" [2]=> string(8) "4/8/2014" [3]=> string(24) "S engineering industries" [4]=> string(9) "Makwanpur" [5]=> string(6) "300000" } [3]=> array(6) { [0]=> string(15) "Contract-2070-9" [1]=> string(8) "3/6/2014" [2]=> string(8) "4/6/2014" [3]=> string(24) "Gourishankar nirman sewa" [4]=> string(8) "Lalitpur" [5]=> string(6) "400000" } [4]=> array(6) { [0]=> string(16) "Contract-2070-10" [1]=> string(8) "2/6/2014" [2]=> string(9) "6/16/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "400000" } }
$file_contract=fopen('contracts.csv','r');
$file_awards=fopen('awards.csv','r');
while(($data=fgetcsv($file_contract))!=FALSE){
$contracts_info[]=$data;
}
while(($data=fgetcsv($file_awards))!=FALSE){
$awards_info[]=$data;
}
$con_count=count($contracts_info);
for($x=0;$x<$con_count;$x++){
if($x==0){
unset($awards_info[0][0]);
$data[$x]=array_merge($contracts_info[0],$awards_info[0]);
}
else{
$check=0;
$award_count=count($awards_info);
for($y=1;$y<$award_count;$y++){
if($awards_info[$y][0]==$contracts_info[$x][0]){
unset($awards_info[$y][0]);
$data[$x]=array_merge($contracts_info[$x],$awards_info[$y]);
$check=1;
}
}
if($check==0){
$data[$x]=$contracts_info[$x];
}
}
}
$final_data=fopen('final.csv','w');
foreach($data as $list_data){
fputcsv($final_data,$list_data);
}
fclose($final_data);
$c=0;
foreach ($data as $list_data){
if(($list_data[1]=='Current') && ($list_data[10]!=NULL))
{
$c+=$list_data[12];
}
}
echo $c;
You can just add the content file in the new file, without parsing files :
$fp = fopen('final.csv','w');
fwrite($fp, file_get_contents('contracts.csv'));
fwrite($fp, file_get_contents('awards.csv'));
fclose($fp);
echo file_get_contents('final.csv');
ok it's a wrong way. So i try to rewrite the loop :
for($x=0; $x < count($contracts_info); $x++) {
$data[$x] = $contracts_info[$x];
if($x == 0) {
if(isset($awards_info[$x])) {
$data[$x] = array_merge($data[$x], $awards_info[$x]);
unset($awards_info[$x]);
}
} else {
foreach($awards_info as $y => $award_info_datas) {
if($award_info_datas[0] == $contracts_info[$x][0]) {
$data[$x] = array_merge($data[$x], $award_info_datas);
unset($awards_info[$y]);
break;
}
}
}
}

Categories