php access a foreach variable in a nested function

Kevin :
function processImages($images)
{
$uploadfiles = array();
// make sure images array does not contain more than 3 images
if (count($images) <= 3) {
    foreach ($images as $image) {
        $uploadfile = "NULL";
        if ($image['size'] != 0) {
            //file process

            function doFileCheck($file, $image)
            {
                $imginfo_array = getimagesize($file);
                /*
getimagesize() only works with images
if getimagesize() fails it will return
false you can use var_dump() to see inside
the array.
echo var_dump($imginfo_array) 
 */

                if ($imginfo_array !== false) {
                    $uploaddir = './uploads/';
                    $uploadfile = $uploaddir . basename($image['name']);

                    if (file_exists($uploadfile)) {
                        echo "<h3>That filename already exists - try again!</h3>";
                    } else {
                        if (move_uploaded_file($image['tmp_name'], $uploadfile)) {
                            echo "<h3>File seems valid, and was successfully uploaded</h3>";
                        } else {
                            echo "INVALID! Possible file upload attack!\n";
                        }
                    }

                    return $uploadfile;
                }
            }

            if ($image['error'] == 0) {
                $file = $image['tmp_name'];
                $file_info = new finfo(FILEINFO_MIME);

                $mime_type_long = $file_info->buffer(file_get_contents($file)); //file_get_contents() reads a file into a string

                $intpos = strpos($mime_type_long, ";"); //get position of the ';'

                //do the string manipulation to get back just what we want
                $mime_type = substr($mime_type_long, 0, $intpos);

                switch ($mime_type) {
                    case 'image/jpeg':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/jpg':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/gif':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/png':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/bmp':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/pjpeg':
                        array_push(doFileCheck($file, $image));
                        break;
                    case 'image/x-png':
                        array_push(doFileCheck($file, $image));
                        break;
                    default:
                        echo "<h1>Not so fast...$mime_type</h1>";
                        break;
                }
            } else {
                echo "<h3>File Size too Large - try again</h3>";
            }

            //end file process
        }
    }
}

return $uploadfiles;
}

I have this file process function, the problem is that I get a an exception that says only variables should be passed by reference. It throws that exception in the switch statement in the 1st case because I am trying to upload a jpeg image but I am debugging it and can't find the problem. I am passing an array to the function that contains each file from the form (3 in total) and then looping through the files to process each one

Aksen P :

Read more about array_push().

You're passing a doFileCheck($file, $image) instead of $array_of_vars and $new_element_in_array_of_vars.

BTW $array_of_vars[] = $new_element_in_array_of_vars; works faster than array_push($array_of_vars,$new_element_in_array_of_vars);.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301576&siteId=1