2011-04-21 71 views
0

我需要一些幫助,請掙扎,用AJAX上傳和PHP

我上傳裝載有一個jQuery上傳的幫助的圖像,使用戶可以得到它的圖像的預覽,我可以jCrop ,我的問題是,然後我需要保存的文件名,但我不能得到我的上傳功能創建變量,

用戶去通過量是如下的方法,

  1. 用戶填寫在他們的細節
  2. 用戶選擇S和圖像擡起負荷並點擊了上傳按鈕
  3. 上傳功能運行,並且該圖像被返回到圖
  4. 用戶選擇將要剪切的圖像的區域,並保存
  5. 用戶進行填充形成了
  6. 保存的物品包括文件名

下面是我的PHP代碼,我相信這是其中變量就不復存在了,不能在add函數中使用,

添加() - 該函數的形式提交過

public function add() 
{ 
    $this->form_validation->set_rules('title','title', 'required|trim|min_length[2]'); 
    $this->form_validation->set_rules('firstname', 'firstname', 'required|trim|alpha'); 
    $this->form_validation->set_rules('surname', 'surname', 'required|trim|alpha'); 
    $this->form_validation->set_rules('email', 'email address', 'required|trim|valid_email'); 
    $this->form_validation->set_rules('postcode', 'postcode', 'required|trim|min_length[6]|max_length[9]'); 
    $this->form_validation->set_rules('company_name', 'company_name', 'required|trim'); 
    $this->form_validation->set_rules('company_summary', 'company_summary', 'required|trim|max_length[3000]'); 
    $this->form_validation->set_rules('alternative_ads', 'alternative ads', 'required|trim|prep_url'); 
    $this->form_validation->set_rules('facebook_url', 'Facebook URL', 'required|trim|prep_url'); 
    $this->form_validation->set_rules('twitter_url', 'Twitter URL', 'required|trim|prep_url'); 

    if($this->form_validation->run() == FALSE) 
    { 
     $this->template->build('admin/users/add'); 
    } 
    else 
    { 
     //group the post data together soe that we can save the data easily. 
     $user = array(
      'firstname' => $this->input->post('firstname'), 
      'surname' => $this->input->post('surname'), 
      'email' => $this->input->post('email'), 
      'postcode' => $this->input->post('postcode'), 
      'date_registered' => date("d-m-y h:i:s", time()) 
     ); 

     if(!$this->users_model->insert($this->input->xss_clean($user))) 
     { 
      $data['error'] = "We could not save you details, please try again"; 
      $this->template->build('/users/admin/add', $data); 
     } 

     $employer = array(
      'company_name' => $this->input->post('company_name'), 
      'company_summary' => $this->input->post('company_summary'), 
      'logo' => $this->file['file_name'], 
      'alternative_ads' => $this->input->post('alternative_ads'), 
      'facebook_url' => $this->input->post('facebook_url'), 
      'twitter_url' => $this->input->post('twitter_url'), 
      'user_id' => $this->db->insert_id() 
     ); 

     if(!$this->employer_model->insert($this->input->xss_clean($employer))) 
     { 
      $data['error'] = "We could not save you details, please try again"; 
      $this->template->build('/users/admin/add', $data);    
     } 
     else 
     { 
      die(print_r($this->file)); 
      $this->load->library('image_lib'); 

      $config['image_library'] = 'gd2'; 
      $config['source_image'] = '/media/uploads/users/' . $this->file['file_name']; 
      $config['thumb_marker'] = TRUE; 
      $config['x_axis'] = $this->input->post('x'); 
      $config['y_axis'] = $this->input->post('y'); 

      $this->image_lib->initialize($config); 

      if (! $this->image_lib->crop()) 
      { 
       $data['error'] = "We could not crop you image, please try again. If the problem persists please contact us"; 
       $this->template->build('/admin/users/add', $data); 
      } 

      $this->session->set_flashdata('success', 'You have successfully added an employer'); 
      redirect('/admin/users/manage'); 
     } 
    } 
} 

這jQuery的上傳者稱

private function upload() 
{ 
    $config['upload_path'] = "./media/uploads/users"; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $config['encrypt_name'] = TRUE; 

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


    if (! $this->upload->do_upload('userfile')) 
    { 
     $error = array('error' => $this->upload->display_errors()); 
     die(print_r($error)); 
    } 
    else 
    { 
     $this->file = $this->upload->data(); 
     $msg = $this->file; 
     echo json_encode($msg); 
    } 
} 

回顧一下我在上傳設置$this->file(),並試圖上傳功能在add()函數中使用它。

回答

1

您必須使用return函數upload()函數才能在add()函數中使用它。根據你的應用程序和它的設置,這可能會或可能不會打破你的ajax。

另一種方法是設置會話,你需要的位。

0

$ this-> file只會在上傳範圍內。所以就像Ross提到的那樣,上傳在json中回顯圖像,然後在表單中對它進行處理。就像將它添加到一個隱藏的輸入,然後與發佈數據一起發送。