2017-02-28 123 views
0

嗨,我想上傳圖像使用codeigniter和ajax ..一旦我上傳圖像的錯誤將顯示您正在嘗試上傳的文件類型是不允許的。我在上傳文件夾中檢查它沒有上傳圖片。我不知道哪裏是我的編碼任何一個u能幫助我的錯誤...使用codeigniter和ajax上傳圖像

這是我的看法編碼

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
?><!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Welcome to CodeIgniter</title> 
     <!-- Latest compiled and minified CSS --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
     <!-- jQuery library --> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
     <!-- Latest compiled JavaScript --> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
     <script> 
      $(document).ready(function(){ 
       $('#upload_form').on('submit',function(e){ 
        e.preventDefault(); 
        if($('#image_file').val()=='') 
        { 
         alert("Pls Select the Image File"); 
        } 
        else{ 
         $.ajax({ 
          url:"<?php echo base_url()?>Upload/uploadFile", 
          method:"POST", 
          data:new FormData(this), 
          contentType:false, 
          catch:false, 
          processData:false, 
          success:function(data){ 
           $('#uploaded_image').html(data); 
          } 
         }); 
        } 

       }); 
      }); 
     </script> 
</head> 
<body> 
<div id="container"> 
    <h3 align="center"><?php echo $title ?></h3> 
     <form enctype="multipart/form-data" id="upload_form" class="formReset" method="post"> 
      <div class="control-group form-group"> 
       <div class="controls"> 
        <label>Upload Photo:</label> 
        <input type="file" name="image_file" id="image_file"> 
        <p class="help-block"></p> 
       </div> 
       </div> 

       <button type="submit" class="btn btn-primary" id="upload" name="upload">Submit</button> 
     </form> 

     <div id="uploaded_image"> 

     </div> 
</div> 
</body> 
</html> 

這是我的控制器編碼

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Upload extends CI_Controller { 
    public function index() 
    { 
     $data['title']="Image Upload funtion using ajax in codeigniter";  
      $this->load->view('Upload',$data); 
    } 
     public function uploadFile(){ 
      if(isset($_FILES['image_file']['name'])){ 
       $config['upload_path']='./upload/'; 
       $config['allowed_types']='jpg/jpeg/png/gif'; 
       $config['overwrite']=TRUE; 
       $config['max_size']='1000000'; 
       $config['max_height']='1000'; 
       $config['max_width']='1000'; 
       $this->load->library('upload', $config); 
       if(!$this->upload->do_upload('image_file')){ 
        echo $this->upload->display_errors(); 
       } 
       else{ 
        $data=$this->upload->data(); 
        echo '<img src="'.base_url().'upload/'.$data["file_name"].'"/>'; 
       } 
      } 
     } 
} 
+0

你沒有得到這個腳本的基礎url值。 – Vimal

+0

問題解決了....我改變了我的編碼$ this-> load-> library('upload',$ config);如果($ this-> upload-> do_upload(「image_file」)){ $ data = array('upload_data'=> $ this-> upload-> data()); echo''; – pixel

回答

0

允許的類型應被管道分隔而不是正斜槓

eg

$config['allowed_types'] = 'gif|jpg|jpeg|png'; 

可以是數組或管道分隔字符串。