String will not convert to array
Posted: Wed Apr 19, 2017 6:07 pm
Hello Everyone,
This ought to be straightforward, I want to retrieve the entire “type” array out of the $_FILES array, so that I can run a test to determine what uploaded files are images. Everything I've looked at in the Manual and at various forums hasn't worked.
Since there are a number of image types, I tried to use preg_grep to find any image files uploaded in the “type” array, and all I could retrieve was, “array.” I tried several other approaches and the most I could retrieve was, “[userfile] => array.” Since this is a multi-dimensional array, I had previously attempted to loop through the array and could only print out the proper keys and values, but couldn't contain them in a variable outside of the loop.
I then tried a foreach loop with a key and value and have been able to grab the values for each upload, image/jpeg, text/plane, or even ' ' (which is really important – I want to unset these.)
But when I attempted to create a mono-dimensional array, I'm having all kinds of trouble. I'm not getting any error message, just garbage output. I created a variable and assigned the formatted data values with single quotations and a comma. Then I removed the last comma and space and even used settype to make sure that the variable contains a string. When I run the code, I get an array with one index [0] and all my values, see webPage.jpg:
Once I have this data as an array, I can use preg_grep to find all the non-images and unset them, as the indices will be exactly the same as the ones in the $_FILES array.
Get this, in order to create the last array in webPage.jpg, I copied and pasted the string from the HTML output, “The string in the Values Var is: ” (in red) into the array() for the variable $cantSayOnTV, and then it can output that to the page as a proper array.
Here's my code. At this point, the insert page simply calls this function:[/syntax]
So, is there a straightforward way to grab all of the “type” array from the $_ FILES array – or is there some way to force the string to be converted into a proper array? Thanks in advance for your assistance!
Cheers,
Rick
This ought to be straightforward, I want to retrieve the entire “type” array out of the $_FILES array, so that I can run a test to determine what uploaded files are images. Everything I've looked at in the Manual and at various forums hasn't worked.
Since there are a number of image types, I tried to use preg_grep to find any image files uploaded in the “type” array, and all I could retrieve was, “array.” I tried several other approaches and the most I could retrieve was, “[userfile] => array.” Since this is a multi-dimensional array, I had previously attempted to loop through the array and could only print out the proper keys and values, but couldn't contain them in a variable outside of the loop.
I then tried a foreach loop with a key and value and have been able to grab the values for each upload, image/jpeg, text/plane, or even ' ' (which is really important – I want to unset these.)
But when I attempted to create a mono-dimensional array, I'm having all kinds of trouble. I'm not getting any error message, just garbage output. I created a variable and assigned the formatted data values with single quotations and a comma. Then I removed the last comma and space and even used settype to make sure that the variable contains a string. When I run the code, I get an array with one index [0] and all my values, see webPage.jpg:
Once I have this data as an array, I can use preg_grep to find all the non-images and unset them, as the indices will be exactly the same as the ones in the $_FILES array.
Get this, in order to create the last array in webPage.jpg, I copied and pasted the string from the HTML output, “The string in the Values Var is: ” (in red) into the array() for the variable $cantSayOnTV, and then it can output that to the page as a proper array.
Here's my code. At this point, the insert page simply calls this function:
Code: Select all
[syntax=php]
function forUpload() {
// Try a different approach
foreach ($_FILES['userfile']['type'] as $key => $value) {
$values .= "'".$value."', ";
}
// remove the last two characters
$values = rtrim($values,', ');
// set the type for the variable
settype($values, "string");
echo "The string in the Values Var is: ".$values."<br />";
echo gettype($values)."<br />";
// TRY to make this an array
$newArray = array($values);
// Try to echo out the simple array here
echo '<pre>';
print_r($newArray);
echo '</pre>';
"<br />";
// force the output to become an array
$cantSayOnTV = array('image/tiff', 'image/png', 'image/gif', '', 'image/jpeg', 'text/plain');
echo '<pre>';
print_r($cantSayOnTV);
echo '</pre>';
"<br />";
echo "Exiting Data Validation on line 218";
exit;
}
So, is there a straightforward way to grab all of the “type” array from the $_ FILES array – or is there some way to force the string to be converted into a proper array? Thanks in advance for your assistance!
Cheers,
Rick