0

多数の入力テキストフィールドと、複数の画像のみを受け入れることができるファイル アップロード入力を含むフォームをここに用意します。

何が起こる必要があります:

一連の画像を選択して [送信] ボタンをクリックした後、コントローラーはテキスト データを 1 つのテーブルに挿入し、すべての画像をアップロードして、挿入されたテキスト データにリンクするforeign_key を使用してファイル名を画像テーブルに追加し、最後に選択された最初の画像がサムネイルに変わり、その名前とテキスト データ行にリンクしているforeign_keyがサムネイルテーブルにもアップロードされます。

何が起こるのですか:

画像とサムネイルは適切にアップロードされます (または、重複などなしにアップロード フォルダーに入れられます) が、何らかの理由で、最初の画像ではなく、選択された最後の画像がサムネイル表示されます。

また、1 つの画像のみがサムネイル化されますが、データベースは、選択/アップロードされたすべての画像の名前を、その名前を含むサムネイル テーブルに追加することに_thumbなります。

crud.php (コントローラー)

function add()
        {
            //Set validation properties
            $this->_set_fields();

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

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

        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
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '6000';
                $config['max_width']  = '1680';
                $config['max_height']  = '1050';
                $path_to_uploads= './assets/upload';
                $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

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

                $_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]
                    );
                }

                //add this
                $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;
                    }else{

                        $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' => $id
                        );

                        //Upload original image
                        $this->crud_model->save_image($image_row);

                        if(current($_FILES) == $_FILES['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');
        }

crud_model.php (モデル)

//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

1 に答える 1

1

チェックが失敗しているように見えるため、毎回ループが最後まで実行され、最後にループされた画像でサムネイルが上書きされます。

からチェックを変更してみてください

if(current($_FILES) == $_FILES['file_0']){

if($h=='file_0'){
于 2012-10-09T18:48:36.793 に答える