我正在制作一个管理工具,用于我正在制作的网站,它允许管理员将图像上传到服务器上的文件夹,该文件夹存储图库的图像。正在正确上载文件,但图像名称未被放置在数据库中。该名称应放在" gallery_images"上的"路径"领域。如何解决这个问题?
我正在使用CakePHP 2.4.4
控制器
<?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);
//}
//}
}
}
?>
查看
<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
?>
答案 0 :(得分:1)
admins
db table 是:
Admin
,您的应用文件:../ app / Model / {{}}}。{/ li>
您的应用文件中的Admin
Controller.php,Admins
。 add_foto()
db table 是:
gallery_images
,您的应用文件: ../ app / Model / GalleryImage
。php ,GalleryImage
Controller.php 详细了解Cake命名约定:http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html
的 1。 - 保存一个表
如果您要将数据保存到GalleryImages
,则必须为gallery_images
创建表单,例如:
查看: ../ app / View / GalleryImages / add_foto.ctp
GalleryImage
CONTROLLER :../ app / Controller / GalleryImagesController.php
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');
的 2。保存许多表格
如果您想同时将数据保存到数据库表public function add_foto() {
// ...
// debug($this->request->data); die(); // << You can see Your data
if($this->request->data){
// ...
$this->GalleryImage->save($this->request->data);
// ...
};
// ...
}
和admins
(一种形式)。你必须使用gallery_images
,
阅读更多:http://book.cakephp.org/2.0/en/models/saving-your-data.html
你必须首先定义模型关系/链接模型:belongs_to / has_many,如:
模型:Admin.php:
$this->YourModelName->saveAll($this->request->data)
模型:GalleryImage.php
var $hasMany = array(
'GalleryImage' => array(
'dependent' => true
),
);
然后查看:... /管理员/ add_foto.ctp
var $belongsTo = array('Admin');
CONTROLLER :../ Controller / AdminsController.php
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');
希望得到这个帮助。
答案 1 :(得分:1)
<强>控制器强>
<?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!'));
}
}
}
}
?>
查看强>
<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');
?>
<强>模型强>
<?php
App::uses('AppModel', 'Model');
class GalleryImage extends AppModel{
public $displayField ='path';
public $useTable = 'gallery_images';
}
?>