0
I’m trying to make a tool for the administrator, the site I’m building, that will allow you to upload images to a folder on the server, so that they appear posthumously in the gallery. The image upload is working correctly, however its name is not entered in the database. This should be inserted in the "gallery_images" table, in the "path" field. How to solve this problem?
I’m using Cakephp 2.4.4
Controller
<?php
class AdminsController extends AppController{
public $components = array('RequestHandler');
public function admin_index(){
if(!$this->Session->check('Admin')){
$this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
$this->redirect(array(
'controller' => 'admins',
'action' => 'login'));
}
$this->layout='admin_index';
}
public function add_foto() {
if(!$this->Session->check('Admin')){
$this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
$this->redirect(array(
'controller' => 'admins',
'action' => 'login'));
}
$this->layout='admin_index';
$file=$this->request->data['gallery_images']['path'];
if($this->request->is('post') || $this->request->is('put')){
$this->Admin->create();
$this->Admin->save($file);
move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
if($this->Admin->save($this->request->data)){
$this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
}
}
//$this->Admin->id = $id;
//$this->Post->save($data=array($this->data['Admins']['path']), $params=array('fieldList'=>'path'));
//$this->Post->saveField('path', $this->data['Admins']['path']);
/*if ($this->ModelName->save($this->request->data)) {
$this->Session->setFlash('Data Saved!');
}*/
//if($this->request->is('post')){
// $this->Admin->save($this->request->data);
//}
//}
}
}
?>
View
<h2>Adicionar Fotografia</h2>
<?php
echo "<br>";
echo $this->Form->create('Admin',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
//echo $this->Form->submit();
echo $this->Form->end('Guardar');
?>
Código Certo
Controller
<?php
class GalleryController extends AppController {
public function admin_upload_image(){
$this->layout = 'admin_index';
if($this->request->is('post') || $this->request->is('put')) {
/* $file = $this->request->data['gallery_images']['path']['name'];*/
$file = array(
'GalleryImage' => array(
'path' => $this->request->data['gallery_images']['path']['name']
)
);
move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
$this->loadModel('GalleryImage');
$this->GalleryImage->create();
if($this->GalleryImage->save($file)){
$this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
}
else{
$this->Session->setFlash(__('Erro ao carregar o ficheiro!'));
}
}
}
}
?>
View
<h2>Adicionar Fotografia</h2>
<?php
echo "<br>";
echo $this->Form->create('GalleryImage',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
//echo $this->Form->submit();
echo $this->Form->end('Guardar');
?>
Model
<?php
App::uses('AppModel', 'Model');
class GalleryImage extends AppModel{
public $displayField ='path';
public $useTable = 'gallery_images';
}
?>
What the Admin->save() function is doing?
– Lucio
@Lucio Should write in the database.
– SunT
You can show the table structure
admin
?– Paulo Rodrigues
@Paulorodrigues already added to the post, in the background.
– SunT