Я пытаюсь загрузить несколько изображений с проверкой, но не могу это сделать.
Мой код выглядит так:
<?php echo form_open("controller/action");?> <ul> <li>Category Image <input type="file" name="category_img"></li> <li>Product Image <input type="file" name="product_img"></li> <li>Slider Image <input type="file" name="slider_img"></li> </ul> <input type="submit" name="submit" class="btn" value="Submit" /> <?php echo form_close();?>
Ждем решения.
Выполняя это по-своему, ваша форма должна быть такой:
<?php // success or error status in uploading for each file if( !empty( $notification ) ) { echo ' <p>Notifications : </p> <p>'.$notification.'</p>'; } ?> <!-- multipart form opening --> <?php echo form_open_multipart("image/upload");?> <!-- "controller/action" --> <ul> <li>Category Image <input type="file" name="category_img"></li> <li>Product Image <input type="file" name="product_img"></li> <li>Slider Image <input type="file" name="slider_img"</li> </ul> <input type="submit" name="submit" class="btn" value="Submit" /> <?php echo form_close();?>
И ваш контроллер должен быть таким:
class Image extends CI_Controller { private $data; // data member for storing status of the uploads function __construct() { // some code } public function index() { $this->upload(); } public function upload() { $this->data['notification'] = ''; if( $this->input->post('submit') ) { // loading helpers $this->load->helper(array('form', 'url')); //setting the config array, for more options see Codeigniter docs $config['upload_path'] = 'uploads/'; // upload path $config['allowed_types'] = 'gif|jpg|jpeg|png'; // allowed file types // loading upload library with config array $this->load->library('upload', $config); // uploading the files, lets_upload() is defined below $this->lets_upload( 'category_img' ); $this->lets_upload( 'product_img' ); $this->lets_upload( 'slider_img' ); } $this->load->view('form', $this->data); // form view is loaded along with success or error notification in the 'data' member variable } public function lets_upload( $field_name ) // '$field_name' refers to input field name { if ( ! $this->upload->do_upload( $field_name )) // if uploading failed { $this->data['notification'] .= $this->upload->display_errors(); // stored error in member variable 'data' } else // if uploading success { $upload_data = $this->upload->data(); // stored the file info in 'data' $this->data['notification'] .= $upload_data['file_name']." is successfully uploaded.<br>"; // file name is displayed with 'success' message } } }
Создайте массив вашего поля ввода, как это
<?php echo form_open_multipart('upload/do_upload');?> <input type="file" multiple name="userfile[]" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form>
и в вашем contrller
function do_upload() { $this->load->library('upload'); $files = $_FILES; $cpt = count($_FILES['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['userfile']['name']= $files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); } } private function set_upload_options() { //upload an image options $config = array(); $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '0'; $config['overwrite'] = FALSE; return $config; }
U добавил в это в category_img
$config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = 250; $this->load->library('upload', $config); $this->upload->do_upload('category_img'); $upload_data = $this->upload->data(); $file_name = $upload_data['file_name']; $this->upload->initialize($config); if (!$this->upload->do_upload('category_img')) { $error = array('error' => $this->upload->display_errors()); $error['error']; } $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = 250; $this->load->library('upload', $config); $this->upload->do_upload('product_img'); $upload_data = $this->upload->data(); $file_name = $upload_data['file_name']; $this->upload->initialize($config); if (!$this->upload->do_upload('product_img')) { $error1 = array('error1' => $this->upload->display_errors()); $error1['error1']; } $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = 250; $this->load->library('upload', $config); $this->upload->do_upload('slider_img'); $upload_data = $this->upload->data(); $file_name = $upload_data['file_name']; $this->upload->initialize($config); if (!$this->upload->do_upload('slider_img')) { $error2 = array('error2' => $this->upload->display_errors()); $error2['error2']; }