2017-08-14 98 views
0

我有一個音頻上傳功能,可以上傳用戶選擇的音樂。 但是,有些文件顯示它們的格式正確,但上傳者說我試圖上傳的文件不正確。PHP文件上傳不允許格式在allowed_types

我笨的配置如下:

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

我使用以下方法來獲取音頻格式:

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

當我回聲$轉出,它顯示的MP3,但上傳帶有此錯誤的退貨:

The filetype you are attempting to upload is not allowed.

當我使用音頻轉換器(我使用Freemake)將fi再次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); 
       } 
      } 
     }); 
    })(); 
+0

發表您完全上傳相關代碼。你在使用CodeIgniter嗎? – jagad89

+0

對不起,忘了提,是的,我使用的是CodeIgniter,我已經添加了完整的PHP代碼 –

+0

你的視角是什麼? – jagad89

回答

1

雖然在codeigniter打開,mimes.php 並添加此。

'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

+0

謝謝!這是一個快速而簡單的答案,你可能介意解釋「application/octet-stream」的含義嗎? –

+1

'application/octet-stream'表示二進制流。它還允許你上傳任何'exe'文件或二進制文件。按照我的說法,這是一個危險的解決方案 – jagad89

+0

你可以標記爲接受或upvote,我不介意:) –