PHP Developers Network
http://forums.devnetwork.net/

String will not convert to array
http://forums.devnetwork.net/viewtopic.php?f=1&t=143532
Page 1 of 1

Author:  rick.emmet [ Wed Apr 19, 2017 6:07 pm ]
Post subject:  String will not convert to array

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: [ Download ] [ Hide ]
[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;
}
[/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

Attachments:
webPage.jpg
webPage.jpg [ 228.36 KiB | Viewed 893 times ]

Author:  Celauran [ Thu Apr 20, 2017 5:55 am ]
Post subject:  Re: String will not convert to array

rick.emmet wrote:
This ought to be straightforward, I want to retrieve the entire “type” array out of the $_FILES array

$_FILES['userfile']['type'] is an array. What are you trying to do here?

Author:  rick.emmet [ Thu Apr 20, 2017 1:05 pm ]
Post subject:  Re: String will not convert to array

Hello Celauran,
Thanks for responding. I have a series of functions that strip tags, check for upload errors, mime type or anything that's black listed. The code creates a new image and destroys the old one. Anyone can copy an HTML form and add userfile buttons to send any number of files to the server. I found that if there's a whole in the userfile type array (such as ' ') the code processes all the subsequent images and places them in the upload folder.

I want to unset these from the $_FILES array and then hand off the remaining files to the functions that filter the input. I would like to use preg_grep() to find all the cases in the userfile array that are not ( ^ ) images. I attempted to use preg_grep on $_FILES, but can't retrieve anything other than “array” so I wanted to create a mono-dimensional array, use preg_grep on it and then unset the corresponding files in the $_FILES array.

The $_ FILES array can then be processed with a limit on the number of photos. The idea is that I won't process thirty or fifty photos, and then limit the number moved to the upload folder. I don't want the processing overhead if I can avoid it.

The text book shows the use of the array construct like this:

$products = array('tires', 'oil', 'spark plugs');

and that worked. A variable is supposed to be exactly what is inside of it, so:

$newArray = array($values);

for instance, should be the same as:

$ newArray = array('image/tiff', 'image/png', 'image/gif', '', 'image/jpeg', 'text/plain');

I just looked up array() in the Manual and it show that one can use the key and value pair or just the values. I tried to create an array with the key value pair in this manner:

$values .= $key." => '".$value."', ";

And I get the output:

Array
(
[0] => 0 => 'image/tiff', 1 => 'image/png', 2 => 'image/gif', 3 => '', 4 => 'image/jpeg', 5 => 'image/jpeg'
)
And again if I cut and past the output in red into the line of code (within the parenthesis):

$cantSayOnTV = array(0 => 'image/tiff', 1 => 'image/png', 2 => 'image/gif', 3 => '', 4 => 'image/jpeg', 5 => 'image/jpeg');

I get a proper array like this:

Array
(
[0] => image/tiff
[1] => image/png
[2] => image/gif
[3] =>
[4] => image/jpeg
[5] => image/jpeg
)


instead of one that has a single index of [0]. The fact that the code can output a duplicate of the userfile type array makes its failure when using a variable seem odd to me. Any help with this is very much appreciated.
Cheers,
Rick

Author:  Celauran [ Thu Apr 20, 2017 2:50 pm ]
Post subject:  Re: String will not convert to array

This is all very convoluted. What are you trying to accomplish here?

Author:  rick.emmet [ Sun Apr 23, 2017 6:43 pm ]
Post subject:  Re: String will not convert to array

Hi Celauran,
Here's what I was trying to do:

Syntax: [ Download ] [ Hide ]
// Create an array of only the file type
$types = array();
foreach ($_FILES as $filename => $arrayofvalues) {
    $types['type'] = $arrayofvalues['type'];
}

// Create a sub-array of types
foreach ($types as $subArray) {
    foreach ($subArray as $val) {
        $simple[] = $val;
    }
}

// Get the key & value of just image files
$images = preg_grep('/\image\b/', $simple);

// Use the differenc array function to find the indices I want
$dif = array_diff($simple, $images);

// Loop through the difference array and uset non-photos
foreach ($dif as $key => $value) {
    unset($_FILES['userfile']['name'][$key]);
    unset($_FILES['userfile']['type'][$key]);
    unset($_FILES['userfile']['tmp_name'][$key]);
    unset($_FILES['userfile']['error'][$key]);
    unset($_FILES['userfile']['size'][$key]);
}


And the output from that is:

Syntax: [ Download ] [ Hide ]
Full FILES array:

Array
(
    [userfile] => Array
        (
            [name] => Array
                (
                    [0] => text.txt
                    [1] =>
                    [2] => Home Page.tiff
                    [3] => video_icon.png
                    [4] => watersports.gif
                    [5] => IMG_20170325_130533573_HDR.jpg
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] =>
                    [2] => image/tiff
                    [3] => image/png
                    [4] => image/gif
                    [5] => image/jpeg
                )
// MORE OF THE $_FILES ARRAY BELOW

All the 'type' array:

Array
(
    [type] => Array
        (
            [0] => text/plain
            [1] =>
            [2] => image/tiff
            [3] => image/png
            [4] => image/gif
            [5] => image/jpeg
        )

)

Simple images array:

Array
(
    [0] => text/plain
    [1] =>
    [2] => image/tiff
    [3] => image/png
    [4] => image/gif
    [5] => image/jpeg
)

the elements to remove:

Array
(
    [0] => text/plain
    [1] =>
)

FILES array after unsetting:

Array
(
    [userfile] => Array
        (
            [name] => Array
                (
                    [2] => Home Page.tiff
                    [3] => video_icon.png
                    [4] => watersports.gif
                    [5] => IMG_20170325_130533573_HDR.jpg
                )

            [type] => Array
                (
                    [2] => image/tiff
                    [3] => image/png
                    [4] => image/gif
                    [5] => image/jpeg
                )
// MORE OF THE $_FILES ARRAY BELOW


This as a first pass on the FILES array. Thanks for your input.
Cheers,
Rick

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/