Phalcon hasFiles() 总是 return true
Phalcon hasFiles() always return true
当检查 if
条件为 if ($this->request->hasFiles() == true)
时,文件上传功能总是返回 true
;即使没有上传任何文件。
当表单为空时收到消息为:
File format not supported
注意: 但是上传图片时工作正常。
HTML
表单是使用 Phalcon tag
创建的。下面是来自 controller
的函数并使用 volt
显示 form
.
控制器:
function imageupload()
{
if ($this->request->hasFiles() == true)
{
$upload_dir = BASE_PATH . '/files/';
foreach ($this->request->getUploadedFiles() as $file) {
if($file->getSize()>1000000)
{
$this->flash->error("File too big");
return false;
}
if(!in_array($file->getRealType(),array('image/jpg','image/jpeg','image/png','image/gif')))
{
$this->flash->error("File format not supported");
return false;
}
if($file->isUploadedFile())
{
$filename=rand().'_'.date('Ymdhis').'.'.$file->getExtension();
$file->moveTo($upload_dir . $filename);
return $filename;
}
else
{
$this->flash->error($file->getError());
return false;
}
}
}
return true;
}
查看:
<?php
echo $this->tag->form(['company/create','enctype'=>'multipart/form-data']);
?>
<p>
<label for="name">Name</label>
<?php echo $this->tag->textField("name"); ?>
</p>
<p>
<label for="logo">Logo</label>
<?php echo $this->tag->fileField("logo"); ?>
</p>
<p>
<p>
<?php echo $this->tag->submitButton("Create"); ?>
</p>
<?php $this->tag->endForm(); ?>
调用上传函数:
<?php
$logoname=$this->imageupload();
if($logoname==false)
{
$this->dispatcher->forward(['action' => 'new']);
}
else
{
......
....
$success = $form->save();
if($success)
{
$this->flash->success("Company successfully saved!");
$this->dispatcher->forward(['action' => 'index']);
}
else
{
$this->flash->error("Following Errors occured:");
foreach($company->getMessages() as $message)
{
$this->flash->error($message);
}
$this->dispatcher->forward(['action' => 'new']);
}
}
?>
通过修改 if
条件,如下所示:
if ($this->request->hasFiles(true) == true)
当检查 if
条件为 if ($this->request->hasFiles() == true)
时,文件上传功能总是返回 true
;即使没有上传任何文件。
当表单为空时收到消息为:
File format not supported
注意: 但是上传图片时工作正常。
HTML
表单是使用 Phalcon tag
创建的。下面是来自 controller
的函数并使用 volt
显示 form
.
控制器:
function imageupload()
{
if ($this->request->hasFiles() == true)
{
$upload_dir = BASE_PATH . '/files/';
foreach ($this->request->getUploadedFiles() as $file) {
if($file->getSize()>1000000)
{
$this->flash->error("File too big");
return false;
}
if(!in_array($file->getRealType(),array('image/jpg','image/jpeg','image/png','image/gif')))
{
$this->flash->error("File format not supported");
return false;
}
if($file->isUploadedFile())
{
$filename=rand().'_'.date('Ymdhis').'.'.$file->getExtension();
$file->moveTo($upload_dir . $filename);
return $filename;
}
else
{
$this->flash->error($file->getError());
return false;
}
}
}
return true;
}
查看:
<?php
echo $this->tag->form(['company/create','enctype'=>'multipart/form-data']);
?>
<p>
<label for="name">Name</label>
<?php echo $this->tag->textField("name"); ?>
</p>
<p>
<label for="logo">Logo</label>
<?php echo $this->tag->fileField("logo"); ?>
</p>
<p>
<p>
<?php echo $this->tag->submitButton("Create"); ?>
</p>
<?php $this->tag->endForm(); ?>
调用上传函数:
<?php
$logoname=$this->imageupload();
if($logoname==false)
{
$this->dispatcher->forward(['action' => 'new']);
}
else
{
......
....
$success = $form->save();
if($success)
{
$this->flash->success("Company successfully saved!");
$this->dispatcher->forward(['action' => 'index']);
}
else
{
$this->flash->error("Following Errors occured:");
foreach($company->getMessages() as $message)
{
$this->flash->error($message);
}
$this->dispatcher->forward(['action' => 'new']);
}
}
?>
通过修改 if
条件,如下所示:
if ($this->request->hasFiles(true) == true)