我需要允许用户上传图片,我希望将该图片保存在目录(webroot / uploads)中,并且我希望数据库存储该文件的路径图片。
我收到此错误:Warning (2): mb_strlen() expects parameter 1 to be string, array given [CORE\src\Validation\Validation.php, line 742]
adaugare.ctp
<div class="masiniSh form large-9 medium-8 columns content">
<?= $this->Form->create($masiniSh, array('enctype'=>'multipart/form-data')) ?>
<fieldset>
<legend><?= __('Adauga anunt') ?></legend>
<?php
echo $this->Form->control('marca', array('required'=>false));
echo $this->Form->control('denumire', array('required'=>false));
echo $this->Form->control('versiune', array('required'=>false));
echo $this->Form->control('combustibil', array('required'=>false));
echo $this->Form->control('cilindree', array('required'=>false));
echo $this->Form->control('putere_maxima', array('required'=>false));
echo $this->Form->control('consum_urban', array('required'=>false));
echo $this->Form->control('consum_extraurban', array('required'=>false));
echo $this->Form->control('consum_mixt', array('required'=>false));
echo $this->Form->control('tractiune', array('required'=>false));
echo $this->Form->control('cutie_viteze', array('required'=>false));
echo $this->Form->control('numar_usi', array('required'=>false));
echo $this->Form->control('pret', array('required'=>false));
echo $this->Form->control('km', array('required'=>false));
echo $this->Form->control('an_fabricatie', array('required'=>false));
echo $this->Form->control('tel', array('required'=>false));
echo $this->Form->control('nume_proprietar', array('required'=>false));
echo $this->Form->control('locatie', array('required'=>false));
echo $this->Form->control('imagine', array('type'=>'file'));
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
MasiniShController.php
public function adaugare()
{
$masiniSh = $this->MasiniSh->newEntity();
if ($this->request->is('post')) {
$masiniSh = $this->MasiniSh->patchEntity($masiniSh, $this->request->getData());
if ($this->MasiniSh->save($masiniSh)) {
$this->Flash->success(__('The ad has been saved..'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The ad could not be saved. Please, try again.'));
}
if (!empty($this->data)) {
//Check if image has been uploaded
if (!empty($this->data['MasiniSh']['imagine']['name'])) {
$file = $this->data['MasiniSh']['imagine']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'png', 'gif'); //set allowed extensions
//only process if the extension is valid
if (in_array($ext, $arr_ext)) {
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'uploads/' . $file['name']);
//prepare the filename for database entry
$this->data['MasiniSh']['imagine'] = $file['name'];
}
}
//now do the save
$this->MasiniSh->save($this->data) ;
}
}
$this->set(compact('masiniSh'));
}
P.S。 imag字段在数据库中设置为varchar(255)
问题是当我点击提交按钮时出现错误。我发现,如果我对来自maxLength('imagine', 255)
的{{1}}发表评论,则此错误会消失,但它会再次出现错误:提供的值无效。
答案 0 :(得分:0)
看来你正在尝试将Cake 3.x代码与一些旧版本混合使用,也许你在网上发现了一些并试图整合它?下面的更新版本带有一些内联注释。请注意,它未经测试,但应该关闭!
public function adaugare()
{
$masiniSh = $this->MasiniSh->newEntity();
// This is the new way of checking for $this->data
if ($this->request->is('post')) {
// Get a copy of the posted data for easier use
$data = $this->request->getData();
//Check if image has been uploaded
// Note: the array keys here may not be exactly right, use pr($data) to look at it and make any required corrections. I've made a guess that MasiniSh isn't needed now.
// Note2: it would be a more "Cake" solution to move this if block to the beforeMarshal function of the MasiniSh table class
if (!empty($data['imagine']['name'])) {
$file = $data['imagine']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'png', 'gif'); //set allowed extensions
//only process if the extension is valid
if (in_array($ext, $arr_ext)) {
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'uploads/' . $file['name']);
//prepare the filename for database entry
$data['imagine'] = $file['name'];
} else {
// You probably want to unset the imagine key from the data here, and maybe delete the temp file?
}
}
//now update the entity and do the save
$masiniSh = $this->MasiniSh->patchEntity($masiniSh, $data);
if ($this->MasiniSh->save($masiniSh)) {
$this->Flash->success(__('The ad has been saved..'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The ad could not be saved. Please, try again.'));
}
}
$this->set(compact('masiniSh'));
}
答案 1 :(得分:0)
Greg Schmidt已经回答了这个问题,但我想补充一些建议。文件属于数据层,最好在Table / Entity类中使用它们。在我的应用程序中,我使用了一种从这个组件cakephp-upload component借来的技术。首先,您需要添加自定义数据库类型。就我而言,它被称为FileType
。然后,您需要将此新类型映射到要存储文件名的字段。这些步骤将使您能够使用beforeSave
方法中的数组来处理字段。它将使您的控制器代码更加清晰,因为您将上传逻辑移动到模型。我已经a Gist for you here了。我希望你能得到这个想法,它可以帮助你完成你的项目。