Function for checking/checking Image Upload

Asked

Viewed 98 times

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);

    }

Pastebin version:

http://pastebin.com/nZcUJEHQ

Mode of 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:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • I suggest that you post the Pastebin code here, many who can help you, may not have access to this site. I myself, at the moment, do not have access. To know what to post from your code, I recommend you read How to create a Minimum, Complete and Verifiable example

  • Entered as informed

  • Without much analysis, I suggested that you remove double quotes from within straight parentheses that contain variable names - $_FILES["$name"]["name"] for $_FILES[$name]["name"] - Do this for the whole script, there are several parts of code where variables appear in quotes within these parentheses.

  • strtolower(end(explode(".",$input['arquivo']))); This here generates a Warning.

  • The problem was not only this, there were others related to logic.

1 answer

0

This line here will return an error, of the type Strict Standards

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

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

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

So instead of this:

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 mimetype in the array. The function array_search, looks in an array for the specified value, but it does not return true when finding this value. Returns the index of this value, which can even be 0 which is the equivalent of false. In that case, your accountant $x never leaves the 0, or even can, if the value of both indices is different from 0.

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

Where have you:

((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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.