将 MeioUpload 与 CakePHP 1.3 结合使用
我正在尝试使用 CakePHP 1.3 中的 MeioUpload 行为 实现一些相当简单的图像上传,但我可以'为了我的一生让它发挥作用。当我尝试在控制器中保存 $this->data
时,它会尝试保存常规文件数组(由于缺乏更好的词)而不仅仅是文件名。
这就是我正在做的:
我已将 meio_upload.php 放入 /app/models/behaviors
在我的模型中,我正在执行以下操作:
var $actsAs = array(
'MeioUpload.MeioUpload' => array(
'filename' => array(
'dir' => 'img{DS}upload{DS}brawlers',
'allowedMime' => array('image/png'),
'allowedExt' => array('.png', '.PNG'),
'zoomCrop' => false,
'thumbsizes' => array(
'normal' => array(
'width' => 150,
'height' => 150
)
),
'default' => 'default.png',
'length' => array(
'minWidth' => 100,
'minHeight' => 100,
'maxWidth' => 150,
'maxHeight' => 150
)
)
)
);
在我看来,我有以下形式:
<?php
echo $this->Form->create('Brawler', array('type' => 'file'));
echo $this->Form->input('name', array(
'label' => 'Name',
'maxLength' => '45'
)
);
echo $this->Form->input('comment', array(
'label' => 'Description',
'rows' => '3'
)
);
echo $this->Form->input('author', array(
'label' => 'Your name)',
'maxLength' => '45'
)
);
echo $this->Form->input('email', array(
'label' => 'Email (will not be shown)',
'maxLength' => '45'
)
);
echo $this->Form->input(
'filename',
array(
'between'=>'<br />',
'type'=>'file',
'label' => 'Image (Max 2mb, 150x150 pixels, .png)'
)
);
echo $this->Form->end('Submit');
?>
最后我的添加操作关联的控制器如下所示:
function add() {
if (!empty($this->data)) {
$this->Brawler->create();
if($this->Brawler->save($this->data)) {
$this->Session->setFlash('The brawler has been saved', true);
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('The Brawler could not be saved. Please try again.', true);
debug($this->data);
debug($this->validationErrors);
die();
//$this->redirect(array('action'=>'add'));
}
}
}
对于后代,这是我的表设计:
delimiter $$
CREATE TABLE `brawlers` (
`id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`comment` text,
`email` varchar(45) NOT NULL,
`author` varchar(45) NOT NULL,
`filename` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8$$
当我尝试提交表单时,这是我得到的输出:
app/controllers/brawlers_controller.php (line 37)
Array
(
[Brawler] => Array
(
[name] => Viking
[comment] => Herp. This is a description.
[author] => Me
[email] => [email protected]
[filename] => Array
(
[name] => 5.png
[type] => image/png
[tmp_name] => /storage/configuration/upload_tmp_dir/phpEF2okD
[error] => 0
[size] => 15863
)
)
)
app/controllers/brawlers_controller.php (line 37)
显然,当它尝试将数组保存到文件名字段时会失败。图像也永远不会保存在指定的上传目录中。似乎 meioupload 行为从未被实际使用过。我如何验证这一点?
您必须原谅我发布的大量代码,但我认为向您展示所有内容比让您猜测我可能在做什么更好。如果有人能发现这个错误,那就可以节省我很多时间去拉扯我的头发。
I'm trying to implement some fairly simple image uploading using the MeioUpload behaviour in CakePHP 1.3, but I can't for the life of me get it to work. When I try to save $this->data
in my controller, it tries to save a regular file array (for lack of a better word) rather than just the filename.
Here's what I'm doing:
I've put meio_upload.php into /app/models/behaviors
In my model, I'm doing the following:
var $actsAs = array(
'MeioUpload.MeioUpload' => array(
'filename' => array(
'dir' => 'img{DS}upload{DS}brawlers',
'allowedMime' => array('image/png'),
'allowedExt' => array('.png', '.PNG'),
'zoomCrop' => false,
'thumbsizes' => array(
'normal' => array(
'width' => 150,
'height' => 150
)
),
'default' => 'default.png',
'length' => array(
'minWidth' => 100,
'minHeight' => 100,
'maxWidth' => 150,
'maxHeight' => 150
)
)
)
);
In my view, I've got the following form:
<?php
echo $this->Form->create('Brawler', array('type' => 'file'));
echo $this->Form->input('name', array(
'label' => 'Name',
'maxLength' => '45'
)
);
echo $this->Form->input('comment', array(
'label' => 'Description',
'rows' => '3'
)
);
echo $this->Form->input('author', array(
'label' => 'Your name)',
'maxLength' => '45'
)
);
echo $this->Form->input('email', array(
'label' => 'Email (will not be shown)',
'maxLength' => '45'
)
);
echo $this->Form->input(
'filename',
array(
'between'=>'<br />',
'type'=>'file',
'label' => 'Image (Max 2mb, 150x150 pixels, .png)'
)
);
echo $this->Form->end('Submit');
?>
And finally my add action in the associated controller looks like this:
function add() {
if (!empty($this->data)) {
$this->Brawler->create();
if($this->Brawler->save($this->data)) {
$this->Session->setFlash('The brawler has been saved', true);
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('The Brawler could not be saved. Please try again.', true);
debug($this->data);
debug($this->validationErrors);
die();
//$this->redirect(array('action'=>'add'));
}
}
}
For posterity, here's my table design:
delimiter $
CREATE TABLE `brawlers` (
`id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`comment` text,
`email` varchar(45) NOT NULL,
`author` varchar(45) NOT NULL,
`filename` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8$
When I try to submit my form, this is the output I get:
app/controllers/brawlers_controller.php (line 37)
Array
(
[Brawler] => Array
(
[name] => Viking
[comment] => Herp. This is a description.
[author] => Me
[email] => [email protected]
[filename] => Array
(
[name] => 5.png
[type] => image/png
[tmp_name] => /storage/configuration/upload_tmp_dir/phpEF2okD
[error] => 0
[size] => 15863
)
)
)
app/controllers/brawlers_controller.php (line 37)
Obviously, this fails when it tries to save an array to the filename field. The image is never saved in the specified upload directory either. It seems like the meioupload behavior is never actually used. How can I verify this?
You'll have to excuse the mass of code that I've posted, but I figure it's better that I show you everything than to have you guess at what I may be doing. If someone can spot the error, that would save me many hours of pulling my hair.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嘿,我有你的查询的答案
,那就是你必须创建另一个变量
$d1 并将 $this->date 数组复制到该数组中,如下所示
$d1['brawlers']['name'] = $this->数据['争吵者']['名称'];
所有变量
$d1['brawlers']['filename'] = $this->data['brawlers']['filename']['name'];
然后像 $this->brawlers->save($d1); 一样保存它
Hey i have answer for ur Query
That is u have to make anther variable Say
$d1 and copy $this->date array into that array like below
$d1['brawlers']['name'] = $this->data['brawlers']['name'];
all ur variables
$d1['brawlers']['filename'] = $this->data['brawlers']['filename']['name'];
and then save it like $this->brawlers->save($d1);
我不是 PHP 人员,但前段时间我不得不使用 CakePHP 和 Meio.Upload。
据我所知,您的数据库中需要 4 个字段来存储图像:
settings)
根据您的错误和数据库模式判断,我会说您缺少一些字段。
编辑:
请参阅文档
I'm NOT PHP guy, but I had to use the CakePHP and Meio.Upload some time ago.
AFAIK you need 4 fields in your database for image:
settings)
Judging on your error and db schema I would say you are missing some of the fields.
Edit:
See the documentation
我遇到了同样的问题,您使用的MeioUpload版本与您的Cake版本不兼容。我结束了使用 MeioUpload 2.3 版本。
I had the same problem, the MeioUpload version your are using is incompatible with your version of Cake. I ended using version 2.3 of MeioUpload.