For + count in CakePHP

1

I'm trying to make my for rotate according to how many images are 'entered' in SELECT. The problem is that regardless of how many images are 'inputted', it only saves the first one. What is wrong with this logic?

private function criarfoto($galeria_id, $file) {
    $extension = "";
    $valores = count($file['Foto']); // SALVA em quantidade,os indices que tem dentro do array.
    for ($i = 0; $i < $valores; $i++) { // faz o loop de acordo com a quantidade de indices no array
        $extension = pathinfo($file['Foto'][$i]['name'], PATHINFO_EXTENSION);
        $allowExt = array('jpg', 'jpeg', 'png', 'gif');
        //print_r($file); 
        if (!in_array($extension, $allowExt)) {
            return false;
        }
        $fotos_galeria = array(
            'galeria_id' => $galeria_id,
            'titulo' => $file['titulo'],
            'descricao' => $file['descricao'],
            'extension' => $extension,
        );
        $this->Foto->create();
        if ($this->Foto->save(array('Foto' => $fotos_galeria))) {
            //die(print_r($fotos_galeria));
            $foto_id = $this->Foto->id;
            if (move_uploaded_file($file['Foto'][$i]['tmp_name'], WWW_ROOT . 'files' . DS . 'galeria' . DS . "{$foto_id}_o.{$extension}")) {
                return $foto_id;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
    
asked by anonymous 20.04.2015 / 21:45

1 answer

1

Instead of using return false when it fails, use continue , because return false will cause the loop to stop when it does not meet the criteria, and continue will cause the loop to go to the next array item.

If you want to identify also the elements that have failed to send, I suggest you change your return type from Boolean to array.

If you need to return the Ids of the inserted images, you will have to work with a two-dimensional array, storing the status and return message in each case.

<?php
private function criarfoto($galeria_id, $file) {
    $extension = "";
    $valores = count($file['Foto']); // SALVA em quantidade,os indices que tem dentro do array.
    $error = array();
    for ($i = 0; $i < $valores; $i++) { // faz o loop de acordo com a quantidade de indices no array
        $extension = pathinfo($file['Foto'][$i]['name'], PATHINFO_EXTENSION);
        $allowExt = array('jpg', 'jpeg', 'png', 'gif');
        //print_r($file); 
        if (!in_array($extension, $allowExt)) {
            $error[] = "Extensão inválida para a imagem {$file['Foto'][$i]['name']}";
            continue;
        }
        $fotos_galeria = array(
            'galeria_id' => $galeria_id,
            'titulo' => $file['titulo'],
            'descricao' => $file['descricao'],
            'extension' => $extension,
        );
        $this->Foto->create();
        if ($this->Foto->save(array('Foto' => $fotos_galeria))) {
            //die(print_r($fotos_galeria));
            $foto_id = $this->Foto->id;
            if (!move_uploaded_file($file['Foto'][$i]['tmp_name'], WWW_ROOT . 'files' . DS . 'galeria' . DS . "{$foto_id}_o.{$extension}")) {
                $error[] = "Falha carregando imagem {$file['Foto'][$i]['name']} para o servidor";
            }
        } else {
             $error[] = "Falha salvando a imagem {$file['Foto'][$i]['name']} no banco de dados";
        }
    }
    return (empty($error)) ? true : $error;
}
?>
    
24.06.2015 / 23:25