2015-05-07 37 views
0

嗨,我有這種形式,我使用ajaxForm提交我的圖像在圖像文件夾。當我試圖上傳圖片時,我已經有了ajaxForm的庫,我注意到我上傳的圖片不會上傳到assets/uploads文件夾中。我試圖print_r路徑,但它顯示圖片的文件名,但不會插入到資產/上傳文件夾,這裏是我下面ajaxForm使用codeigniter插入圖像到圖像文件夾不工作

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
    <script> 
     jQuery(document).ready(function($){ 
      $("#uploadPhoto").change(function(){ 
       var val = $(this).val(); 
       switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){ 
        case 'gif': case 'jpg': case 'png': case 'jpeg': 
        $("#ptxt").fadeOut(); 
        $("#buttonGallery").show(); 
        break; 
        default: 
        $(this).val(''); 
        // error message here 
        $("#ptxt").fadeIn(); 
        $("#buttonGallery").hide(); 
        break; 
       } 
       }); 

       $('#send_form').ajaxForm({ 
        target: '.result', 
        success: function(data) { 

        }  
       }); 


     }); 



    </script> 



<form id="send_form" enctype="multipart/form-data" action="<?php echo base_url().'admin/rooms/addGallery'?>" method="post" >       
          <div class="row"> 
          <div class="col-md-4"> 
           <div><h3>Gallery</h3></div> 
                  <div class="alert alert-danger" id="ptxt"> 
                   <p>Invalid file type</p> 
                  </div> 
           <div class="form-group"> 
           <label for="uploadPhoto">Upload photo</label> 
           <input type="file" name="uploadPhoto" id="uploadPhoto" placeholder="Enter "> 
           </div> 
           <div class="form-group"> 
           <label for="photoTitle">Photo title</label> 
           <input type="text" name="photoTitle" class="form-control" id="photoTitle" placeholder="Photo title " > 
           </div> 
           <div class="form-group"> 
           <label for="photoDescription">Photo description</label> 
           <textarea name="photoDescription" class="form-control" id="photoDescription" placeholder="Photo description " rows="5"></textarea> 
           </div>                
          </div>        
          </div> 
                <input type="hidden" name="galleryId" id="galleryId" value="<?php echo $getRoomDetails->id; ?>"> 
          <button type="submit" name="buttonGallery" id="buttonGallery" class="btn btn-primary">Save</button> 
         </form> 

我控制器

public function addGallery(){ 
     if($this->upload->do_upload('uploadPhoto')){ 
      $data['upload_data'] = $this->upload->data();   

      $uploadSuccess = $data['upload_data']; 

      $raw_name = $uploadSuccess['file_name']; 

      $this->load->library('image_lib'); 

      $file_name = $raw_name; 

      $this->load->library('image_lib'); 

      $image_path = 'assets/uploads/' .$file_name; 
      echo $image_path; exit; 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = $image_path; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['width'] = 150; 
      $config['height'] = 110; 
      $config['new_image'] = 'thumb_'.$file_name; 
      $this->image_lib->initialize($config); 
      $this->image_lib->resize(); 
      $this->image_lib->clear(); 
     } 


    } 

我加在public function __construct一個配置視圖只是爲了確保

public function __construct(){ 
     parent::__construct(); 
     $config['upload_path'] = 'assets/uploads/'; 
    // set the filter image types 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '9000'; 
    $config['encrypt_name'] = TRUE; 

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

    $this->upload->initialize($config); 
    $this->upload->set_allowed_types($config['allowed_types']); 
    $data['upload_data'] = ''; 


    } 

有人可以幫助我想通了這件事了?TIA

+0

你檢查了文件夾權限 –

回答

0

有問題,你的控制器文件

你上傳你的配置之前設置

控制器代碼將是

<?php 
public function addGallery(){ 
$data['upload_data'] = $this->upload->data(); 

$uploadSuccess = $data['upload_data']; 

$raw_name = $uploadSuccess['file_name']; 

$image_path = 'assets/uploads/' . $file_name; 
$config['upload_path'] = 'assets/uploads/'; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['max_size'] = '100'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 

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

if (!$this->upload->do_upload()) { 
    $error = array('error' => $this->upload->display_errors()); 

    $this->load->view('upload_form', $error); 
} else { 
    $data['upload_data'] = $this->upload->data();   

      $uploadSuccess = $data['upload_data']; 
      $raw_name = $uploadSuccess['file_name']; 
      $file_name = $raw_name; 
      $this->load->library('image_lib'); 

      $image_path = 'assets/uploads/' .$file_name; 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = $image_path; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['width'] = 150; 
      $config['height'] = 110; 
      $config['new_image'] = 'thumb_'.$file_name; 
      $this->image_lib->initialize($config); 
      $this->image_lib->resize(); 
      $this->image_lib->clear(); 
} 
} 

,並從控制器構造函數刪除上傳的一部分,你的文件

+0

謝謝我已經解決了:) – user1718865

0

先給你

$配置[「upload_path」]

的完整路徑,我認爲,你需要給全路徑才能保存圖像。要獲得完整的路徑,你可以使用函數

GETCWD()

讓你的當前工作目錄,以及您需要Concat的文件夾的剩餘。

在加載幫助程序之前,還要初始化您的配置數組。

+0

檢查我的編輯並初始化你的配置變量,然後加載你的幫手 –