使用 Zend Form 上传图像无法确定图像扩展名

发布于 2024-12-11 04:11:22 字数 981 浏览 0 评论 0原文

我无法确定使用 Zend Framework 上传的图像的文件类型。 每次我尝试使用 $_FILES['File']['type'] 确定文件类型时,我总是得到applicationoctet-stream。只有在检查表单是否已发布之前确定类型时,我才能获得正确的扩展名。有谁知道为什么?有更好的方法来处理这个问题吗?

这有效:

$form = new Form_Reg;
$this->view->Reg = $form;
if(isset($_FILES['File']['type'])){
    $filetype = $file = $_FILES['File']['type'];
    }

        if ($this->getRequest()->isPost()){

            $formData  = $this->_request->getPost();
            if($form->isValid($formData)){

                echo $filetype;
                exit;


        }
    }

这不起作用:

    $form = new Form_Reg;
$this->view->Reg = $form;




        if ($this->getRequest()->isPost()){

            $formData  = $this->_request->getPost();
            if($form->isValid($formData)){
                $filetype = $file = $_FILES['File']['type'];
                echo $filetype;
                exit;


        }
    }

I'm having trouble determining the file-type of an image I uploaded with Zend Framework.
Everytime I try to determine the filetype with $_FILES['File']['type'], I always get applicationoctet-stream. Only when I determine the type before checking if the form is posted do I get the right extension. Does anyone know why? Is there a nicer way of dealing with this?

this works:

$form = new Form_Reg;
$this->view->Reg = $form;
if(isset($_FILES['File']['type'])){
    $filetype = $file = $_FILES['File']['type'];
    }

        if ($this->getRequest()->isPost()){

            $formData  = $this->_request->getPost();
            if($form->isValid($formData)){

                echo $filetype;
                exit;


        }
    }

This doesn't work:

    $form = new Form_Reg;
$this->view->Reg = $form;




        if ($this->getRequest()->isPost()){

            $formData  = $this->_request->getPost();
            if($form->isValid($formData)){
                $filetype = $file = $_FILES['File']['type'];
                echo $filetype;
                exit;


        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2024-12-18 04:11:22

我在 stackoverflow 上看到了更多有关此问题的问题,但找不到解决我的特定问题的答案。问题是 zend form 无法验证上传的文件是否是图像,一旦它开始处理表单,文件类型就会变成 applicationoctet-stream,无论上传什么类型的文件。

诀窍是调整 php.ini,查找并取消注释:extension=php_fileinfo.dll。

我仍然想知道 zend_form 需要 fileinfo 做什么, $_FILES 全局在没有 fileinfo 的情况下工作。

I've seen more questions about this on stackoverflow but couldn't find the answer that solved my particular problem. The problem was that zend form couldn't validate if the uploaded file was an image, as soon as it started processing the form the filetype would change into applicationoctet-stream, no mather what kind of file was uploaded.

The trick was to adjust the php.ini, look for this and uncomment it: extension=php_fileinfo.dll.

Still I wonder what zend_form does that fileinfo is needed, the $_FILES global works without fileinfo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文