Image Upload Check / Check Function

2

My code:

   //return > 0 is ok!
    function _fileinput($name) {
        var_dump($name);
        $input['arquivo']        = $_FILES["$name"]["name"];
        $input['arquivo']        = strtolower(str_replace("h","",$input['arquivo']));
        $input['mimetype']       = $_FILES["$name"]["type"];
        $input['temp']           = $_FILES["$name"]["tmp_name"];
        $input['mimetypes']      = array("image/jpeg", "image/png");
        $input['extencoes']      = array(".jpg", ".jpeg", ".png");
        $input['extencao']       = strtolower(end(explode(".",$input['arquivo'])));
        $input['mimetype']        = strtolower($input['mimetype']);
        $input['novo']           = "[fi]"."[".date('Y-m')."]"."[".strtolower($name)."]"."[".rand(1000,9999)."]".".".$input['extencao'];

        var_dump($input);

        $x              = 0;
        if ((array_search($input['extencao'], $input['extencoes']) !== FALSE) AND (array_search(mimetype, $input['mimetypes']) !== FALSE)) {
            $x++;
        }
        else {
            $x = 0;
        }
        var_dump($x);

        list($largura, $altura) = getimagesize($input['temp']);
        if ($largura == "" || $altura == "") {
            $x = 0;
        }
        else { 
            $x++; 
        }
        var_dump($x);

        if ($x == 2) {
            return $input['novo'];
        }
        else {
            return 0;
        }
        var_dump($x);

    }

Paste Version:

link

How to use:

$fileinput = _fileinput("foto");
if ($fileinput > 0) {
        echo $fileinput; //nome para o arquivo
}
else {
    echo "deny upload!";
}

Problem:

Does not return the new name for the file, the function in pastebin does not work, why?

Code References:

    
asked by anonymous 13.11.2015 / 12:12

1 answer

0

This line here will return an error of type Strict Standards

strtolower(end(explode(".",$input['arquivo'])));
  

Strict Standards: Only variables should be passed by reference in: ... line

It means that you only have to pass a true variable, not a function that returns an array.

So, instead:

strtolower(end(explode(".",$input['arquivo'])));

Do this:

$input['ext'] = explode(".",$input['arquivo']);
strtolower('.' . end($input['ext']));

Another part is the condition you used to search for the extension and MIME type in the array. The array_search function looks in an array for the specified value, but it does not return true when it finds this value. Returns the index of this value, which can even be 0 which is the equivalent of false . In this case, your counter $x never comes out of 0 , or you can even if the value of both indexes is different from 0 .

The correct would be to use the function, in_array , which checks whether a value exists in an array, and returns true or false in case it does not find this value.

Where you have:

((array_search($input['extencao'], $input['extencoes']) !== FALSE) AND (array_search(mimetype, $input['mimetypes']) !== FALSE))

Do this:

((in_array($input['extencao'], $input['extencoes'])) AND (in_array($input['mimetype'], $input['mimetypes'])))

With the changes made the script would look like this:

function _fileinput($name) {

    $input['arquivo']        = $_FILES[$name]["name"];
    $input['arquivo']        = strtolower(str_replace("h","",$input['arquivo']));
    $input['mimetype']       = $_FILES[$name]["type"];
    $input['temp']           = $_FILES[$name]["tmp_name"];
    $input['mimetypes']      = array("image/jpeg", "image/png");
    $input['extencoes']      = array(".jpg", ".jpeg", ".png");
    $input['ext']            = explode("." , $input['arquivo']);
    $input['extencao']       = strtolower('.'.end($input['ext']));
    $input['mimetype']        = strtolower($input['mimetype']);
    $input['novo']           = "[fi]"."[".date('Y-m')."]"."[".strtolower($name)."]"."[".rand(1000,9999)."]".".".$input['extencao'];



    $x = 0;
    if ((in_array($input['extencao'], $input['extencoes'])) AND (in_array($input['mimetype'], $input['mimetypes']))) {
        $x+=1;
    }
    else {
        $x = 0;
    }


    list($largura, $altura) = getimagesize($input['temp']);
    if ($largura == "" || $altura == "") {
        $x = 0;
    }
    else { 
        $x++; 
    }

    if ($x == 2) {
        return $input['novo'];
    }
    else {
        return 0;
    }

}

print _fileinput('ficheiro');

Some References:

16.11.2015 / 14:16