首页 文章

PHP文件上传不允许使用allowed_types格式

提问于
浏览
0

我有一个音频上传功能,可以上传用户选择的音乐 . 但是,某些文件显示它们的格式正确,但上传者说我尝试上传的文件不正确 .

我的CodeIgniter配置如下:

$config['allowed_types'] = 'mp3|wav|m4a|wma';

我使用以下内容来获取音频的格式:

$ext = end(explode(".", $_FILES["userfile"]["name"]));

当我回显$ ext时,它显示mp3,但上传的返回时出现此错误:

不允许您尝试上传的文件类型 .

当我使用音频转换器(我使用Freemake)将文件再次转换为mp3时,上传器允许该文件,因此我不确定该文件是否显示mp3但仍然是文件系统上的旧格式 .

任何信息/帮助将不胜感激 .

Thanx提前 .


UPDATE

我的PHP上传代码如下所示:

$entry_num = $this -> input -> post('entry_num');
    $location = $this -> input -> post('location');

    $foldername = "./music/$location";

    if (!file_exists($foldername))
    {
        mkdir($foldername, 0777, true);
    }

    $ext = end(explode(".", $_FILES["userfile"]["name"]));

    $config['upload_path'] = $foldername.'/';
    $config['allowed_types'] = 'mp3|wav|m4a|wma';
    $config['file_name'] = $entry_num.'.'.$ext;
    $config['overwrite'] = TRUE;

    $this -> load -> library('upload', $config);

    if (!$this -> upload -> do_upload('userfile'))
    {
        $errors = array('error' => $this -> upload -> display_errors());
        $return = implode(" ",$errors);
    }

我的视图如下所示:HTML

<div class="control-group" id="load-music">
            <label for="grouping_style" class="control-label">Load MP3 Music</label>
            <div class="controls">
                <input type="file" name="userfile">
            </div>
        </div>

JavaScript的

(function() 
    {
        var bar = $('.bar');
        var percent = $('.percent');
        var status = $('#status');
        var progressbar = $('#progressbar');

        $('#mp3form').ajaxForm({
            beforeSubmit : function(data) {
                check = data[1].value;
            },
            beforeSend: function() {
                progressbar.show();
                status.empty();
                var percentVal = '0%';
                bar.width(percentVal)
                percent.html(percentVal);
            },
            uploadProgress: function(event, position, total, percentComplete) {
                var percentVal = percentComplete + '%';
                bar.width(percentVal)
                percent.html(percentVal);

                if (percentComplete > 10)
                {
                    percent.show();
                }
            },
            complete: function(xhr) {                               
                if (xhr.responseText == '"success"')
                {                                       
                    $('#modal-footer-content').html("<button class='btn btn-default' onclick='cancel()'>Close</button>");
                    $('#modal-text').html('Music sucessfully uploaded, thank you.');

                    //Insert code to update table row with music play back

                    comp_code = '<?=$entry_details['location'];?>';
                    entry_num = <?=$entry_details['entry_num'];?>;

                    $('').postJsonCheck('comp/get_entry_details', {comp_code:comp_code, entry_num:entry_num},function(data){
                        var entry_details = data.entry_details;
                        var content = '<div style="float:left;padding-top:5px;"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="15" height="15"><PARAM NAME=movie VALUE="<?=base_url();?>img/audioplay.swf?file=<?=base_url();?>comps/music/'+entry_details["location"]+'/'+entry_details["entry_num"]+'.'+entry_details["music_ext"]+'&auto=no&sendstop=yes&repeat=0&buttondir=<?=base_url();?>img/audiobuttons/green_small&bgcolor=0xffffff&mode=playstop"><PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE=transparent><embed src="<?=base_url();?>img/audioplay.swf?file=<?=base_url();?>comps/music/'+entry_details["location"]+'/'+entry_details["entry_num"]+'.'+entry_details["music_ext"]+'&auto=no&sendstop=yes&repeat=0&buttondir=<?=base_url();?>img/audiobuttons/green_small&bgcolor=0xffffff&mode=playstop" quality=high wmode=transparent width="15" height="15" align="" TYPE="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></div><a href="#" onclick="edit_entry_music(\'<?= $entry_details['location']; ?>\', \'<?= $entry_details['entry_num']; ?>\');return false;" style="margin-left:5px; padding-top:15px;"><i class="fa fa-music"></i></a>';

                        if (entry_details.audio_status == 'Error')
                        {
                            content = '<div style="float:left;padding-top:2px; color: red; padding-right: 7px;"><i class="fa fa-exclamation-triangle" data-original-title="Music to long! Length: '+entry_details.length_text+', Max Length: '+entry_details.max_length_text+'" data-placement="top" data-toggle="tooltip"></i></div>'+content;

                            var notification = {};
                            notification["type"] = 'error';
                            notification["text"] = 'Music added is too long!';
                            notification["title"] = 'Error';

                            show_notification(notification)
                        }

                        $('#edit_<?= $entry_details['entry_num']; ?>_music').html(content);
                        $('[data-toggle="tooltip"]').tooltip({'placement': 'top'});
                    });
                }
                else
                {
                    str = xhr.responseText;
                    var res = str.slice(4, -6); 

                    $('.music_upload_error').html(res);
                    $('.music_upload_error').show();

                    //alert('There was an error updating the music, please try again or contact the administrator. '+xhr.responseText);
                }
            }
        }); 
    })();

1 回答

  • 1

    在codeigniter打开时,mimes.php并添加此项 .

    'mp3'  =>  array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3', 'application/octet-stream')
    

    application / octet-stream表示二进制流 . 内容类型为“application / octet-stream”的MIME附件是二进制文件 . 通常,它将是必须在应用程序中打开的应用程序或文档,例如电子表格或文字处理程序

相关问题