douchui4459 2017-09-05 09:20
浏览 114
已采纳

Phalcon hasFiles()总是返回true

file upload function is always returning true when checking if condition as if ($this->request->hasFiles() == true); even if didn't upload any file.

When the form is empty getting message as:

File format not supported

Note: But working fine when image is uploaded.

HTML form is created using Phalcon tag. Below is the function from controller and using volt to display form.

Controller:

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;
}

View:

        <?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(); ?>

Call upload function:

<?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']);
        }
    }
    ?>
  • 写回答

1条回答 默认 最新

  • douhu2525 2017-09-05 12:31
    关注

    by modifying if condition as below works as expected:

    if ($this->request->hasFiles(true) == true)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题