I am making an admin tool, for the site I am making, that allows the admin to make image uploads to a folder at the server, that stores images for a gallery. The file is being uploaded correctly but the image name isn't being placed on the database. The name should be placed at the table "gallery_images", on the "path" field. How can this be fixed?
I am 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');
//validação é feita no AdminsController
?>
admins
db table is:
Admin
, your app file: ../app/Model/ Admin
.php, Admins
Controller.php, add_foto()
in Your controller AdminsController.php. gallery_images
db table is:
GalleryImage
, your app file: ../app/Model/ GalleryImage
.php , GalleryImages
Controller.php Read more about Cake naming conventions: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html
1. - SAVE ONE TABLE
If You want to save data to gallery_images
, You have to create form for GalleryImage
, like:
VIEW: ../app/View/GalleryImages/ add_foto.ctp
echo $this->Form->create('GalleryImage', array('type' => 'file')); // << db table gallery_images // ... echo $this->Form->input('admin_id', array('value' => $admin_id)); // << id of db table admins // ... echo $this->Form->file('path'); // << your field of db table gallery_images // ... echo $this->Form->end('Guardar');
CONTROLLER : ../app/Controller/ GalleryImagesController.php
public function add_foto() {
// ...
// debug($this->request->data); die(); // << You can see Your data
if($this->request->data){
// ...
$this->GalleryImage->save($this->request->data);
// ...
};
// ...
}
2. SAVE MANY TABLES
If You want to save data to db table admins
and gallery_images
in the same time (one form) . You have to use $this->YourModelName->saveAll($this->request->data)
, read more: http://book.cakephp.org/2.0/en/models/saving-your-data.html
and You have to define model relations/link models: belongs_to/has_many first, like:
MODEL : Admin.php:
var $hasMany = array(
'GalleryImage' => array(
'dependent' => true
),
);
MODEL : GalleryImage.php
var $belongsTo = array('Admin');
Then VIEW : .../Admins/ add_foto.ctp
echo $this->Form->create('Admin', array('type' => 'file')); // << db table gallery_images
// ...
echo $this->Form->input('Admin.id', array('value' => $admin_id)); // << id of db table admins
echo $this->Form->input('Admin.name');
echo $this->Form->input('Admin.surname');
// ...
echo $this->Form->input('GalleryImage.0.admin_id', array('value' => $admin_id)); // << id of db table admins
echo $this->Form->file('GalleryImage.0.path'); // << your field of db table gallery_images
// ...
echo $this->Form->input('GalleryImage.1.admin_id', array('value' => $admin_id)); // << id of db table admins
echo $this->Form->file('GalleryImage.1.path'); // << your field of db table gallery_images
// ...
echo $this->Form->end('Guardar');
And CONTROLLER : ../Controller/ AdminsController.php
public function add_foto() {
// ...
if($this->request->data){
// ...
$this->Admin->saveAll($this->request->data);
// ...
};
// ...
}
Hope this help.
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';
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.