Does anyone know where the error is in this code?

0

The error that is generated is as follows:

  

Notice: Only variables should be passed by reference in

Can you tell me how to solve it? According to the error, it is in this part:

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

Follow 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;
    
asked by anonymous 05.09.2018 / 00:51

1 answer

2

The error is in the use of end() .

The manual is your friend:

mixed end ( array &$array )
  

link

The function specifically expects a array passed by reference ( & ) but you are passing an expression.

One solution would be to save the result of the explode to a variable so that the reference works:

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

But it is still a bad use of end() , since PHP has its own functions to know parts of filename:

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

link


Taking advantage, you have no reason to array_search , just do this:

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

link

    
05.09.2018 / 01:06