首页 > 解决方案 > 代码点火器不允许上传视频文件

问题描述

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
include_once(APPPATH . "vuforiaApi/required/vuforiaclient.php");
class Main extends CI_Controller {

public function UploadImage()
{
    $Filename = time();
    $contentType;
    $contentUrl;
    if(isset($_FILES["ImageTarget"]["name"]))
    {

        $config['file_name'] = $Filename;
        $config['upload_path'] = './uploads/imagetargets';
        $config['allowed_types'] = 'jpg|gif|png|jpeg|JPG|PNG';
        $this->UploadFile($config,'ImageTarget',1);

    }
    else
    {
        echo 'Please select an Image to upload';
    }


    if(isset($_FILES["Content"]["name"]))
    {
        $contentType = $_POST["content_type"];
        if($_POST["content_type"]=="Video"){
        $configF['max_size'] = '0';
        $configF['upload_path'] = './uploads/content/videos';
        $configF['allowed_types'] = 'mp4|avi|mpg|mpeg|wmv';
        $configF['file_name'] = $Filename;
        $this->UploadFile($configF,'Content',2);
        $contentUrl = '/uploads/content/videos/'.$_FILES["Content"]["name"];
        }
        else if($_POST["content_type"]=="3D Asset"){
        $configF['upload_path'] = './uploads/content/assetbundles';
        $configF['file_name'] = $Filename;
        $configF['max_size'] = '0';
        $configF['allowed_types'] = 'unity3d';
        $this->UploadFile($configF,'Content',3);
        $contentUrl = '/uploads/content/assetbundles/'.$_FILES["Content"]["name"];
        }
        else
        {
            echo 'Please select content to upload';
        }

    }
}

public function sendTarget()
{
    //send target to vuforia site
}

public function UploadFile($cfg,$file,$filetype)
{
    $this->load->library('upload', $cfg);


    if ( ! $this->upload->do_upload($file))
    {
            $error = array('error' => $this->upload->display_errors());
            if($filetype == 1)
            echo "Image Target must be an image<br>";
            if($filetype == 2){
            print_r($error);
            print_r($this->upload->file_type);}
            if($filetype == 3)
            echo "Content must be an asset bundle<br>";
    }
    else
    {
            //$data = array('upload_data' => $this->upload->data());
            if($filetype == 2 || $filetype == 3)
            {
                //do something
            }
    }
}

}

当我尝试上传图像和视频文件时,图像已上传,但视频文件 (mp4,avi) 出现错误

Array ( [error] =>
The filetype you are attempting to upload is not allowed.
) 

mimes.php 有正确的条目

'mp4'   =>  'video/mp4',
'avi'   =>  array('video/x-msvideo', 'video/msvideo', 'video/avi', 
'application/x-troff-msvideo'),

$this->upload->file_type

video/mp4

用于 mp4

对于 avi,它给出了 'video/x-msvideo'

我什至尝试在 mp4 和 avi mime 类型中添加“application/octet-stream”

不知道怎么回事!!

标签: phphtmlcodeigniter

解决方案


我想也许我不能使用相同的上传库实例,所以我在 UploadFile($cfg,$file,$filetype) 函数中添加了一行代码以在一切完成后取消设置库

所以现在函数看起来像这样

    public function UploadFile($cfg,$file,$filetype)
{
    $this->load->library('upload', $cfg);

    if ( ! $this->upload->do_upload($file))
    {
            $error = array('error' => $this->upload->display_errors());
            if($filetype == 1)
            echo "Image Target must be an image<br>";
            if($filetype == 2){
            print_r($error);
            print_r($this->upload->file_type);}
            if($filetype == 3)
            echo "Content must be an asset bundle<br>";
    }
    else
    {
            //$data = array('upload_data' => $this->upload->data());
            if($filetype == 2 || $filetype == 3)
            {
                //do something
            }
    }
    unset($this->upload);
} 

现在一切正常


推荐阅读