【问题标题】:struggling with a ajax upload and PHP为ajax上传和PHP而苦苦挣扎
【发布时间】:2011-04-21 09:19:04
【问题描述】:

我需要一些帮助,

我在 jquery 上传器的帮助下上传加载图像,以便用户可以预览图像并且我可以对其进行 jCrop,我的问题是我需要保存文件名,但我不能获取我在上传函数中创建的变量,

用户经历的过程如下,

  1. 用户填写详细信息
  2. 用户选择要上传的图片并点击上传按钮
  3. 上传功能运行,图片返回视图
  4. 用户选择要裁剪和保存的图像区域
  5. 用户填写表单
  6. 保存包含文件名的项目

下面是我的 PHP 代码,我相信这是变量丢失的地方,不能在 add 函数中使用,

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() 函数中使用它。

【问题讨论】:

    标签: php oop codeigniter jquery


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      $this->file 只会在上传的范围内。因此,就像罗斯提到的那样,让上传在 json 中回显图像,然后在表单中对其进行处理。就像将其添加到隐藏的输入中,然后与发布数据一起发送。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 2013-02-20
        • 2021-10-22
        • 1970-01-01
        • 2019-01-20
        • 1970-01-01
        相关资源
        最近更新 更多