doulaozhi6835 2017-08-14 06:45
浏览 195
已采纳

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

I have an audio upload function that uploads music that the user selects. However, some of the files shows they are in the correct format, but the uploader says that the file I am trying to upload is not correct.

My CodeIgniter config is as follows:

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

I use the following to get the format of the audio:

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

When I echo $ext out, it shows mp3, but the uploaded returns with this error:

The filetype you are attempting to upload is not allowed.

When I use an audio converter (I use Freemake) to convert the file to mp3 again, the uploader allows the file, so I am not sure if the file displays mp3 but is still the old format on the file system.

Any information/help would be greatly appreciated.

Thanx in advance.


UPDATE

My PHP upload code looks like this:

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

My View looks like this: 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条回答 默认 最新

  • dongpei3245 2017-08-14 06:51
    关注

    While in codeigniter opens, mimes.php and add this.

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

    application/octet-stream means binary stream. A MIME attachment with the content type "application/octet-stream" is a binary file. Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?