CodeIgniter:如何使这个多上载代码更简单

时间:2014-12-12 06:11:35

标签: php codeigniter

我在CodeIgniter中进行图像多重上传,这是我的代码。这段代码很有用,但我想让它变得更简单,而且我一直在尝试使用' foreach',并且收到了很多错误。请帮助,谢谢。

function do_upload()
{

    if (!empty($_FILES['userfile']['name']))
    {
        $config['upload_path']   = './upload/';
        $config['allowed_types'] = 'jpg|gif|png';
        $config['max_size']      = '1000';
        $config['max_width']     = '1024';
        $config['max_height']    = '768';

        $config['encrypt_name']  = FALSE;
        $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                $error = $this->upload->display_errors();

                $viewData = array(
                'page_info' => array(
                    'title' => 'Trip',
                    'content' => 'trip/trip'
                ),
                'error' => $error,
                'form' => array(
                    'title'       => $this->input->post('title'),
                    'duration'     => $this->input->post('duration'),
                    'trip_date'     => $this->input->post('trip_date'),                         
                    'price'       => $this->input->post('price'),
                    'status'           => $this->input->post('status'),
                    'place_detail'     => $this->input->post('place_detail'),
                    'package_detail'    => $this->input->post('package_detail'),
                    'itinerary'    => $this->input->post('itinerary'),
                    'latitude'  => $this->input->post('latitude'),
                    'longitude'  => $this->input->post('longitude')

                    )
                );
                $viewDataObject = json_decode(json_encode($viewData));
                $this->load->view('template/index', $viewDataObject);
            }
            else
            {
                $uploadedData = $this->upload->data();

                $title = $this->input->post('title');
                $duration     = $this->input->post('duration');
                $trip_date     = $this->input->post('trip_date');
                $price     = $this->input->post('price');
                $status = $this->input->post('status');
                $place_detail = $this->input->post('place_detail');
                $package_detail = $this->input->post('package_detail');
                $itinerary = $this->input->post('itinerary');
                $latitude  = $this->input->post('latitude');
                $longitude  = $this->input->post('longitude');
                $this->m_admin->add_trip($title, $duration, $trip_date, $price, $status, $place_detail, $uploadedData, $package_detail, $itinerary, $latitude, $longitude );

                redirect('admin/trip');
            }
    }

    if (!empty($_FILES['userfile2']['name']))
    {
        $config['upload_path']   = './upload/';
        $config['allowed_types'] = 'jpg|gif|png';
        $config['max_size']      = '1000';
        $config['max_width']     = '1024';
        $config['max_height']    = '768';

        $config['encrypt_name']  = FALSE;
        $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile2'))
            {
                $error = $this->upload->display_errors();

                $viewData = array(
                'page_info' => array(
                    'title' => 'Trip',
                    'content' => 'trip/trip'
                ),
                'error' => $error,
                'form' => array(
                    'title'     => $this->input->post('title'),
                    'duration'     => $this->input->post('duration'),
                    'trip_date'     => $this->input->post('trip_date'),
                    'price'     => $this->input->post('price'),
                    'status' => $this->input->post('status'),
                    'place_detail' => $this->input->post('place_detail'),
                    'package_detail'    => $this->input->post('package_detail'),
                    'itinerary'    => $this->input->post('itinerary'),
                    'latitude'  => $this->input->post('latitude'),
                    'longitude'  => $this->input->post('longitude')

                )
            );
                $viewDataObject = json_decode(json_encode($viewData));
                $this->load->view('template/index', $viewDataObject);
            }
            else
            {
                $uploadedData = $this->upload->data();

                $title = $this->input->post('title');
                $duration     = $this->input->post('duration');
                $trip_date     = $this->input->post('trip_date');
                $price    = $this->input->post('price');
                $status = $this->input->post('status');
                $place_detail = $this->input->post('place_detail');
                $package_detail = $this->input->post('package_detail');
                $itinerary = $this->input->post('itinerary');
                $latitude  = $this->input->post('latitude');
                $longitude  = $this->input->post('longitude');
                $this->m_admin->add_trip($title, $duration, $trip_date, $price, $status, $place_detail, $uploadedData, $package_detail, $itinerary, $latitude, $longitude );

                redirect('admin/trip');
            }
    } 

    if (!empty($_FILES['userfile3']['name']))
    {
        $config['upload_path']   = './upload/';
        $config['allowed_types'] = 'jpg|gif|png';
        $config['max_size']      = '1000';
        $config['max_width']     = '1024';
        $config['max_height']    = '768';

        $config['encrypt_name']  = FALSE;
        $this->upload->initialize($config);


            if ( ! $this->upload->do_upload('userfile3'))
            {
                $error = $this->upload->display_errors();

                $viewData = array(
                'page_info' => array(
                    'title' => 'Trip',
                    'content' => 'trip/trip'
                ),
                'error' => $error,
                'form' => array(
                    'title'     => $this->input->post('title'),
                    'duration'     => $this->input->post('duration'),
                    'trip_date'     => $this->input->post('trip_date'),
                    'status' => $this->input->post('status'),
                    'price'     => $this->input->post('price'),
                    'place_detail' => $this->input->post('place_detail'),
                    'package_detail'    => $this->input->post('package_detail'),
                    'itinerary'    => $this->input->post('itinerary'),
                    'latitude'  => $this->input->post('latitude'),
                    'longitude'  => $this->input->post('longitude')

                )
            );
                $viewDataObject = json_decode(json_encode($viewData));
                $this->load->view('template/index', $viewDataObject);
            }
            else
            {
                $uploadedData = $this->upload->data();

                $title = $this->input->post('title');
                $duration     = $this->input->post('duration');
                $trip_date     = $this->input->post('trip_date');
                $price    = $this->input->post('price');
                $status = $this->input->post('status');
                $place_detail = $this->input->post('place_detail');
                $package_detail = $this->input->post('package_detail');
                $itinerary = $this->input->post('itinerary');
                $latitude  = $this->input->post('latitude');
                $longitude  = $this->input->post('longitude');
                $this->m_admin->add_trip($title, $duration, $trip_date, $price, $status, $place_detail, $uploadedData, $package_detail, $itinerary, $latitude, $longitude);

                redirect('admin/trip');
            }
    }


}

1 个答案:

答案 0 :(得分:0)

如果您使用

,我不明白为什么会出现错误
foreach(array('userfile', 'userfile2', 'userfile3') as $filename) {
    if (!empty($_FILES[$filename]['name'])) {
        ...
        if ( ! $this->upload->do_upload($filename)) {
        ...
    } 
}

您能否提供有关系统显示的错误的更多信息?