Does anyone know where the error in this code is?

Asked

Viewed 58 times

0

The error that is generated me is the following:

Notice: Only variables should be passed by Ference in

Can anyone tell me how to fix it? According to the error, it’s in this part:

$extensoes_aceitas = array('bmp' ,'png', 'svg', 'jpeg', 'jpg');
$extensao = end(explode('.', $_FILES['foto']['name']));

Follows the code...

if ($acao == 'incluir'):

            $nome_foto = 'padrao.jpg';
            if(isset($_FILES['foto']) && $_FILES['foto']['size'] > 0):  

                $extensoes_aceitas = array('bmp' ,'png', 'svg', 'jpeg', 'jpg');
                $extensao = end(explode('.', $_FILES['foto']['name']));

                 // Validamos se a extensão do arquivo é aceita
                if (array_search($extensao, $extensoes_aceitas) === false):
                   echo "<h1>Extensão Inválida!</h1>";
                   exit;
                endif;

1 answer

2


The error is in the use of end().

The manual is your friend:

mixed end ( array &$array )

http://php.net/manual/en/function.end.php

The function specifically expects a array referenced (&) but you’re passing an expression.

A solution would be to store the explode result in a variable for the reference to work:

$partes = explode('.', $_FILES['foto']['name'])
$extensao = end($partes);

But still it’s a misuse of end(), because PHP has its own functions to know filename parts:

$partes = pathinfo($_FILES['foto']['name']);
$extensao = $partes['extension'];

http://php.net/pathinfo


Taking advantage, there’s no reason to make a array_search, that’s all it takes:

if (!in_array($extensao, $extensoes_aceitas)):

http://php.net/manual/en/function.in-array.php

  • Tell me something, this way I can upload other files like pdf for example?

  • Upload you get from qq thing, this your code only runs after the upload done. The most that it serves is for you to discard something that you do not want after upado.

Browser other questions tagged

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