0

我有一个表单,允许我将图像和一堆文本数据上传到数据库表中。图像通过外键连接到文本数据。

问题是如果表单在上传图像时遇到问题 - 文本数据仍然会被插入(如果它通过自己的验证)。

有什么方法可以首先验证图像,如果一切正常,然后将文本数据连同图像的文件名插入相应的表中?

控制器:

function addDataRow()
        {
            //Set common properties
            $data['title'] = 'Add new data row';
            $data['action'] = site_url('crud/addDataRow');
            $data['link_back'] = anchor('crud/index/', 'Back to list', array('class' => 'back'));

            //Set validation properties
            $this->_set_fields();
            $this->_set_rules();

            //Run validation
            if($this->form_validation->run() == FALSE)
            {
                $data['message'] = '';
            }
            else
            {
                //Get the text data from $_POST
                $data_row = array(
                    'title' => $this->input->post('title'),
                    'text' => $this->input->post('text'),
                    'price' => $this->input->post('price'),
                    'status' => $this->input->post('status'),
                    'type' => $this->input->post('type')
                );

                //Insert text data into table
                $id = $this->crud_model->save($data_row);

                //Now move on to image processing
                //original image upload settings
                $path_to_uploads= './assets/upload';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '6000';
                $config['max_width']  = '1920';
                $config['max_height']  = '1920';
                $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                $arr_files = @$_FILES['thumb'];

                $_FILES = array();
                foreach(array_keys($arr_files['name']) as $h){
                    $_FILES["file_{$h}"] = array(
                        'name' => $arr_files['name'][$h],
                        'type' => $arr_files['type'][$h],
                        'tmp_name' => $arr_files['tmp_name'][$h],
                        'error' => $arr_files['error'][$h],
                        'size' => $arr_files['size'][$h]
                    );
                }

                //Initialize upload
                $this->upload->initialize($config);

                foreach(array_keys($_FILES) as $h){

                    if (!$this->upload->do_upload($h)){
                        $error = $this->upload->display_errors();
                        //echo "<script>alert($error);</script>";
                        print($error); die;
                        //Fix this part
                    }else{
                        //Start uploading               
                        $upload_data = $this->upload->data();
                        $file_name = $upload_data['file_name'];
                        $full_file_path = $path_to_uploads.'/'.$file_name;



                        //Insert image data into table
                        $image_row = array(
                        'id_path' => $file_name,
                        'id_data_row' => $id
                        );

                        $this->crud_model->save_image($image_row);

                        if($h=='file_0'){
                            //Thumbnail config
                            $config['image_library'] = 'gd2';
                            $config['source_image'] = $full_file_path;
                            $config['create_thumb'] = TRUE;
                            $config['maintain_ratio'] = TRUE;
                            $config['width'] = 150;
                            $config['height'] = 150;

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

                            $this->image_lib->resize();

                            $thumbnail_row = array(
                            'id_path' => str_replace(".", "_thumb.", $file_name),
                            'id_data_row' => $id
                            );

                            $this->crud_model->save_thumbnail($thumbnail_row);
                        }       
                    }
                }
                //Set form input name="id"
                $this->form_validation->id = $id;

                //Set user message
                $data['message'] = '<div class="success">New data row added!</div>';
            }

            $this->load->view('templates/header', $data);
            $this->load->view('pages/crud_edit', $data);
            $this->load->view('templates/footer');
        }

模型:

//Add new data row
        function save($data)
        {

            $this->db->insert($this->tbl_data, $data);
            return $this->db->insert_id();
        }

        //Add the original image
        function save_image($data)
        {
            $this->db->insert($this->tbl_images, $data);
            return $this->db->insert_id();
        }

        //Add the thumbnail upload path and id of the row in data table to link them
        function save_thumbnail($data)
        {
            $this->db->insert($this->tbl_thumbnails, $data);
            return $this->db->insert_id();
        }
4

3 回答 3

1

这个想法很简单:
首先上传图片,并检查错误。
如果有,则中断该过程。否则在数据库中插入文本数据。

编辑
对于多个文件,您在foreach(array_keys($_FILES) as $h).
您上传一张图片,如果通过,您将图片和文本数据保存在数据库中,然后移动到下一张图片。

foreach(array_keys($_FILES) as $h){
  // Upload the image
  // Check if it succeeded
  // If yes, store the image and
  // store the textual data for it
  // proceed to next image
}

编辑 2
根据您的最后评论,您应该这样做(伪代码):

$temp_images = array(); // This array will hold the uploaded images (their path).
foreach(array_keys($_FILES) as $h){
  // Upload the image
  // Check if it succeeded
  // If yes, store it and add it to the $temp_images array and proceed to next image
  // If it did not, delete all the images in $temp_images and halt process
}
// You have all the images here
// Store the images and their textual data

对不起,我现在无法编写实际代码。我不在我自己的电脑上。

于 2012-10-10T15:12:33.073 回答
1

您也可以在上传到数据库后做一个简单的检查。如果图片不存在,请删除条目。这样您就不需要更改任何内容,只需添加一个 ckeck 和删除部分。

于 2012-10-10T15:25:57.173 回答
1

只需将所有数据推送到数组中,然后等待图像完成上传,然后再将它们插入数据库:

<?php
function addDataRow()
{
    //Set common properties
    $data['title'] = 'Add new data row';
    $data['action'] = site_url('crud/addDataRow');
    $data['link_back'] = anchor('crud/index/', 'Back to list', array('class' => 'back'));

    //Set validation properties
    $this->_set_fields();
    $this->_set_rules();

    //Run validation
    if($this->form_validation->run() == FALSE)
    {
        $data['message'] = '';
    }
    else
    {
        //Get the text data from $_POST
        $data_row = array(
            'title' => $this->input->post('title'),
            'text' => $this->input->post('text'),
            'price' => $this->input->post('price'),
            'status' => $this->input->post('status'),
            'type' => $this->input->post('type')
        );



        //Now move on to image processing
        //original image upload settings
        $path_to_uploads= './assets/upload';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '6000';
        $config['max_width']  = '1920';
        $config['max_height']  = '1920';
        $config['upload_path'] = $path_to_uploads;
        $this->load->library('upload', $config);

        $arr_files = @$_FILES['thumb'];

        $_FILES = array();
        foreach(array_keys($arr_files['name']) as $h){
            $_FILES["file_{$h}"] = array(
                'name' => $arr_files['name'][$h],
                'type' => $arr_files['type'][$h],
                'tmp_name' => $arr_files['tmp_name'][$h],
                'error' => $arr_files['error'][$h],
                'size' => $arr_files['size'][$h]
            );
        }

        //Initialize upload
        $this->upload->initialize($config);

        //Array for image_row data
        $image_row_array = array();

        //array to hold errors
        $error = array();

        foreach(array_keys($_FILES) as $h){

            if (!$this->upload->do_upload($h)){
                $error[$h] = $this->upload->display_errors();
                //echo "<script>alert($error);</script>";
                print($error); die;
                //Fix this part
            }else{
                //Start uploading               
                $upload_data = $this->upload->data();
                $file_name = $upload_data['file_name'];
                $full_file_path = $path_to_uploads.'/'.$file_name;



                $image_row = array(
                    'id_path' => $file_name,
                    'id_data_row' => NULL,//$id isn't set yet
                );

                //push image_row data into array instead of saving to db
                $image_row_array[$h] = $image_row;

                if($h=='file_0'){
                    //Thumbnail config
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $full_file_path;
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 150;
                    $config['height'] = 150;

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

                    $this->image_lib->resize();

                    $thumbnail_row = array(
                    'id_path' => str_replace(".", "_thumb.", $file_name),
                    'id_data_row' => NULL,//$id isn't set yet
                    );
                }       
            }
        }


        //if $error array is empty, images uploaded Ok so add data to db

        if(count($error)==0)
        {
            //Insert text data into table
            $id = $this->crud_model->save($data_row);

            //insert image row data
            foreach($image_row_array as $k => $v)
            {
                //set id
                $image_row_array[$k]['id_data_row'] = $id;

                //if($k != 'file_0')... if you don't want file_0 in there
                $this->crud_model->save_image($image_row_array[$k]);
            }

            //set thumbnail id
            $thumbnail_row['id_data_row'] = $id;

            //insert thumbnail data
            $this->crud_model->save_thumbnail($thumbnail_row);

        }

        //Set form input name="id"
        $this->form_validation->id = $id;

        //Set user message
        $data['message'] = '<div class="success">New data row added!</div>';
    }

    $this->load->view('templates/header', $data);
    $this->load->view('pages/crud_edit', $data);
    $this->load->view('templates/footer');
}
于 2012-10-10T17:56:47.067 回答