2011-04-21 110 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中回显图像,然后在表单中对它进行处理。就像将它添加到一个隐藏的输入,然后与发布数据一起发送。