I have created some basic script to upload images over http/s and as all of you know that working with $_FILES GLOBAL array is a bit uncomfortable, so I decided to write a reArranger
for $_FILES GLOBAL array. I have written two functions for doing the same thing. but one of them is not working properly. I can't get familiar with that misunderstanding. could u help me to find out my mistake!?
openserver 5.9, php7.2 apache2.4
This is an index
<!DOCTYPE html>
<html>
<head>
<title>Upload and Display post images</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<style>
</style>
<body>
<form action="receive.php" method="post" enctype="multipart/form-data" id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
Send these files:<br />
<input name="userfile[]" type="file" multiple="multiple" id="files"/><br /><br />
<input name="upload" id="submitUpload" type="submit" value="Send files" />
</div>
</form>
<br />
<br />
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<script>
</script>
<script src="script.js"></script>
</body>
</html>
last one is upload.php
/* this function gets all images' names, and other variables in good format */
public function getImages($img = array()){
#$html = file_get_contents('phpinfo.php');
#printf($html);
#$html = substr($html, 4, 0);
#return $html;
for($i = 0; $i < @count($img['userfile']['name']); $i++) {
$this->img_arr[$i]['name'] = $img['userfile']['name'][$i];
$this->img_arr[$i]['type'] = $img['userfile']['type'][$i];
$this->img_arr[$i]['tmp_name'] = $img['userfile']['tmp_name'][$i];
}
return $this->img_arr;
}
protected function reArrangeFiles(array $files){
foreach($files['userfile'] as $name => $value){
#echo $name;
for($i=0;$i<count($files['userfile'][$name]);$i++ ){
$rearranged[$i][$name] = $files['userfile'][$name][$i];
}
}
return $rearranged;
}
in upload class there are 2 funcs that rearrange $_FILES's elements but one of them(it is reArrangeFiles) does not return correctly. what is wrong with my func?
I want to get the same result as I get from getImages method. I have cut other part of code because while creating question I was asked not write whole file.
I rewrote function and now it should be working as you want it :)
If you don't want to return them in numerical array, replace $rearranged[$filename][$k] = $value;
with $rearranged[][$k] = $value;
protected function reArrangeFiles(array $files){
foreach ($files['userfile'] as $k => $filesArray) {
foreach($filesArray as $filename => $value) {
$rearranged[$filename][$k] = $value;
}
}
return $rearranged;
}