【问题标题】:Limit File Types in CakePHP File Input在 CakePHP 文件输入中限制文件类型
【发布时间】:2012-08-23 02:11:45
【问题描述】:

我只是想知道是否有办法限制文件输入对话框只显示某些类型的文件。我的网页只能接受 .bin 或 .gz 文件类型,但用户可以选择其他文件类型并尝试上传。

防止上传错误文件的最佳方法是什么?

这是我的文件上传控制器:

    public function uploadFile()
    {
        $this->Session->write('isFileUpload', false);
        $this->Session->write('isFileLarge', false);

        if($this->request->is('post'))
        {
            $uploadedFile = array();

            // Check if the Document object is set
            // If it is set, process the file for uploading,
            if(isset($this->request->data['Document']))
            {
                $filename = $this->request->data['Document']['MyFile']['tmp_name'];

                $uploadedFile['MyFile']['name'] = $this->request->data['Document']['MyFile']['name'];
                $uploadedFile['MyFile']['type'] = $this->request->data['Document']['MyFile']['type'];
                $uploadedFile['MyFile']['size'] = $this->request->data['Document']['MyFile']['size'];

                // Move the file to the /home/spectracom folder
                $filePath = DS . 'home' . DS . $uploadedFile['MyFile']['name'];

                if (move_uploaded_file($filename, $filePath))
                {
                    $this->Session->write('isFileUpload', true);
                    $this->Session->write('isFileLarge', false);
                    $this->redirect('/tools/upgradebackup');
                }
                else
                {
                    $this->Session->write('isFileUpload', false);
                    $this->Session->write('isFileLarge', true);
                    $this->redirect('/tools/upgradebackup');
                }
            }
            else
            {
                $this->Session->write('isFileUpload', false);
                $this->Session->write('isFileLarge', true);
                $this->redirect('/tools/upgradebackup');
            }
        }
    }

我基本上检查文件是否存在,否则它太大,当它返回主升级页面时它会设置会话变量。

谢谢

【问题讨论】:

    标签: file cakephp upload mime file-type


    【解决方案1】:

    您可以使用accept attribute 限制浏览器允许用户在文件选择对话框中选择的内容,尽管并非所有浏览器都支持它。

    我认为这应该适用于创建输入(您需要使用 MIME 类型来查看哪些有效):

    echo $this->Form->input('MyFile', array('type' => 'file', 'options' => array('accept' => 'application/gzip,application/gzipped,application/octet-stream')));

    您还应该通过设置validation on your model 验证文件到达服务器后(查看extensionmimeType 验证规则)。

    您还可以在用户选择文件扩展名后使用 JavaScript 验证文件扩展名,如果扩展名错误,则清除文件输入字段。

    【讨论】:

    • 我也遇到过。我在服务器端进行验证,检查正确的文件类型。但我也想知道如何限制它的客户端。我只是希望人们只能看到某些文件类型。
    • 在做了一些快速研究后,我刚刚更新了答案。在可用的情况下使用接受属性,然后使用 JS 和模型验证。
    • 谢谢。你知道如何将接受融入其中吗? echo $this->Form->input('MyFile', array('type' => 'file'));
    • 我认为列表文件类型会有所帮助:api.cakephp.org/1.3/source-class-MediaView.html#24-83
    • 还没有工作。我用 CakePHP 2.5.7 尝试上面的 sn-p 代码,但没有工作。让我们看看 html 源代码渲染了什么 sn-p 代码。
    【解决方案2】:

    用 Cakephp 3.4 测试

    $this->Form->control('my_file', ['label' => 'Upload File','type' => 'file', 'accept' => 'application/msword']);
    

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多