努力与ajax上传和PHP

时间:2011-04-21 09:19:04

标签: php oop codeigniter jquery

我需要一些帮助,

我上传在jquery上传器的帮助下加载图像,以便用户可以预览图像,我可以jCrop就可以了,我的问题是我需要保存文件名,但我不能获取我在上传功能中创建的变量

用户通过的流程如下,

  1. 用户填写其详细信息
  2. 用户选择并将图像加载并点击上传按钮
  3. 运行上传功能,图像返回到视图
  4. 用户选择将被裁剪并保存的图像区域
  5. 用户继续填写表格
  6. 保存包含文件名
  7. 的项目

    下面是我的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);
        }
    }
    

    总结我在upload()中设置$this->file并尝试在add()函数中使用它。

2 个答案:

答案 0 :(得分:1)

您必须return来自upload()函数的内容才能在add()函数中使用它。根据您的应用程序和它的设置,这可能会或可能不会打破你的ajax。

另一种方法是为你需要的位设置会话。

答案 1 :(得分:0)

$ this->文件只会在上传范围内。就像Ross提到的那样,上传回显json中的图像,然后在表单中用它做一些事情。就像将它添加到一个隐藏的输入,然后与post数据一起发送。