【发布时间】:2016-08-05 14:53:23
【问题描述】:
我是 CakePHP 的新手,我正在使用这个框架创建一个应用程序,现在我遇到了一点问题,不是上传图像,而是图像名称保存到数据库。在我的代码下方:
public function save()
{
$shop = TableRegistry::get('shop');
$query = $shop->query();
//
$product = $this->request->data('prductname');
$domain = $this->request->data('domainname');
$phone = $this->request->data('phone');
if (!empty($this->request->data))
{
$file = $this->request->data['file'];
}
$filename = $file;
$file_tmp_name = $filename;
$dir = WWW_ROOT.'img'.DS.'uploads';
$allowed = array('png','jpg','jpeg' );
if (!in_array(substr(strrchr($filename,'.'),1),$allowed))
{
throw new InternalErrorException("Error Processing Request", 1);
}
elseif(is_uploaded_file($file_tmp_name))
{
$filename = Text::uuid().'-'.$filename;
$fileDB = TableRegistry::get('shop');
$entity = $fileDB->newEntity();
$entity->filename = $filename;
$fileDB->save($entity);
move_uploaded_file($file_tmp_name, $dir.DS.$filename);
}
$query->insert([
'prductname','domainname','phone','file'
])->values([
'prductname'=>$product,
'domainname'=>$domain,
'phone'=>$phone,
'file' => $file
])->execute();
if ($query)
{
$this->Flash->success('This information has been saved.');
$this->redirect(['controller'=>'shop','action'=>'index']);
}
}
表格:
<?php
echo $this->Form->create(null,['url'=>['controller'=>'Shop', 'action'=>'save']],['enctype' => 'multipart/form-data']);
?>
<div class="form-group">
<label for="product">Product Name</label>
<input type="text" class="form-control" name="prductname" placeholder="Name">
</div>
<div class="form-group">
<label for="domain">Domain Name</label>
<input type="text" name="domainname" class="form-control" placeholder="Domain Name">
</div>
<div class="form-group">
<label for="Phone">Phone</label>
<input type="text" name="phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<label for="InputFile">File input</label>
<input type="file" name="file">
</div>
<button type="submit" class="btn btn-default">Save</button>
<?php
echo $this->Form->end();
?>
未显示任何错误,图像名称保存到数据库但未将图像替换到目标文件夹。
谢谢
【问题讨论】:
-
能否提供
move_uploaded_file();函数的代码?
标签: php cakephp cakephp-3.0