0
I am passing this an array containing data from a form, to a function, but in php accuses this error. Cannot use string offset as an array.
public function add($galeria_id) {
if ($this->request->is('post')) {
foreach ($this->request->data['Foto'] as $dados){
$this->criarfoto($galeria_id, $dados);
}
}
$this->render('form');
}
private function criarfoto($galeria_id, $file){
$extension = pathinfo($file['imagem']['name'],PATHINFO_EXTENSION);
$allowExt = array(
'jpg',
'jpeg',
'png',
'gif',
);
if(!in_array($extension, $allowExt)){
return false;
}
$fotos_galeria = array(
'galeria_id' => $galeria_id,
'hot' => $file['hot'],
'titulo' => $file['titulo'],
'descricao' => $file['descricao'],
'extension' => $extension,
);
$this->Foto->create();
if ($this->Foto->save($fotos_galeria)){
//continuação..
By the way, he is not setting the indices of the other fields, the problem was this, when I give a print_r
in $file
it returns me a structure like this:
Galeria de testesmichelakasdkasdasdArray
[name] => compre34.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php8f7w6H
[error] => 0
[size] => 25799
and the right one would have to be a structure like this:
Array
[titulo] => Galeria de testes,
[descricao] => michelakasdkasdasd,
[name] => compre34.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php8f7w6H
[error] => 0
[size] => 25799
Have more details of this error? Which file, the error line, the complete log Cakephp displays on the screen?
– Paulo Rodrigues
Error: Cannot use string offset as an array File: /var/www/intercambioglobal.com.br/app/Plugin/Gallery/Controller/Fotoscontroller.php line 34:
$extension = pathinfo($file['imagem']['name'],PATHINFO_EXTENSION);
– Diego Krashowisk
The point is, I cannot pass an array as a function parameter, it is possible to do this?
– Diego Krashowisk
Did you guarantee that
$file['imagem']['name']
is really a string? Try putting in avar_dump()
before to be sure.– Paulo Rodrigues
return of my vardump:
string(8) "cape.jpg"
– Diego Krashowisk
It is likely that the error is not in this function then. While saving, try it like this:
$this->Foto->save(array('Foto' => $fotos_galeria))
.– Paulo Rodrigues
Take a look at my question again.
– Diego Krashowisk
Solved, when I passed the
$this->request->data['Foto']
, he was passing all the direct argument, and as he only has one form, I passed only the$this->request->data
, and then he passed me all indices with his values.– Diego Krashowisk