0

我已经为多张图片上传创建了表单。但无法使用该表单。只有一张图片上传..我希望将多张图片上传到文件夹并将图片名称保存在数据库中..

我的查看文件

<html>
<head>
<title>Product Upload</title>
<style>
#container
{
    width:750px;
    margin:0 auto;
    //border:5px solid #000000;
}
#container form input[type="text"] {
    height:30px; 
}
</style>
</head>
<body>
<div id="container" align="center">
<form name="product" action="<?php echo base_url;?>admin/login/upload" method="POST" enctype="multipart/form-data" class="form-horizontal">
<table>
<h3>Add New Product</h3>
<tr><td>Categories</td><td><select name="catid"><option>Categories</option>
<?php if(isset($category_details))
{foreach($category_details as $keys=>$values){ ?>
<option value="<?php echo $values['cat_id'];?>"><?php echo $values['cat_name'];?></option>
<?php }
}?>
</select></td></tr>
<tr>
    <td>Product Name:</td><td><input type="text" name="pname"></td></tr>
    <tr><td><input type="file" multiple="true" name="userfile" size="20" /></td></tr>
    <tr><td><br>Product Image:</td><td><br><input type="file" name="pimage[]" id="pimage" multiple></td></tr>
    <tr><td><br>Description:</td><td><br><textarea name="pdescription"></textarea></td></tr>
    <tr><td><br>Price:</td><td><br><input type="text" name="price"></td></tr>
    <tr><td colspan="2" align="center"><br><input type="submit" name="submit" value="ADD" class="btn btn-primary"></td></tr>
</table>
</form>
</div>
</body>
</html>

我的控制器文件

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */

    public function __construct()
     {
        parent::__construct();
       $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
       $this->load->model('admin/category_model');
       $this->load->model('admin/loginauth_model');
       $this->load->model('login_model');
       $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('encrypt');
       $this->load->library('image_CRUD');
     }

    public function index()
    {

        $this->load->view('admin/login');

    }
    public function loadproduct()
    {
        $category_details=$this->category_model->getCategoryDetails();
        $outputdata["category_details"]=$category_details;
        $this->load->view('admin/product',$outputdata); 
   }

 public function loginAuth()
 {
        $this->form_validation->set_rules('email','Enter Email','required|trim|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password','Enter your Password','required|trim|max_length[50]|xss_clean');

        if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/login');
            }
           else
            {
                $username=$_POST['email'];  
            $password=$_POST['password'];

               $user_details=$this->loginauth_model->logincheck($username,$password);
               //print_r($checkauth);
               if($user_details)
                 {
                    if($this->session->userdata('adminusername'))
                    {
                        $adminusername=$this->session->userdata('adminusername');
                        $outputdata['username']=$adminusername;
                    }
                       $category_details=$this->category_model->getCategoryDetails();

                        $outputdata['category_details']=$category_details;
                    $this->load->view('admin/dashboard',$outputdata);
            }
           }
         }

    public function category()
    {
        //$this->load->view('admin/category');
        $this->form_validation->set_rules('cat_name','category name','required|trim|max_length[50]|xss_clean');
      $this->form_validation->set_rules('cat_desc','category description','required|trim|max_length[50]|xss_clean');

        if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/category');
            }
           else
            {
               $addcategory=$this->category_model->addcategory($_POST);
               if($addcategory)
             {

               //**************************pending
             $category_details=$this->category_model->getCategoryDetails();

               $outputdata['category_details']=$category_details;
              //print_r($outputdata);
            $this->load->view('admin/categorylist',$outputdata);
             }
        }
    }
    public function categorylist()
    {
                    //echo image_url;
       $category_details=$this->category_model->getCategoryDetails();

        $outputdata['category_details']=$category_details;
        $outputdata['image_url']=image_url;

        $this->load->view('admin/categorylist',$outputdata);
        $this->load->view('admin/category');

 }
public function userdetails()
    {
        $user_details=$this->login_model->userdetails();

        $outputdata['user_details']=$user_details;

        $this->load->view('admin/userdetails',$outputdata);     

    }
 public function upload()
        {
            $productname=$_POST["pname"];
        $description=$_POST["pdescription"];
            $price=$_POST["price"];
            $catid=$_POST["catid"];
        $name_array = array();
     echo $count = count($_FILES['pimage']['size']);
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++) {
        $_FILES['pimage']['name']=$value['name'][$s];
        $_FILES['pimage']['type']    = $value['type'][$s];
        $_FILES['pimage']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['pimage']['error']       = $value['error'][$s];
        $_FILES['pimage']['size']    = $value['size'][$s];   
            $config['upload_path'] = FCPATH.'img/product_uploads/original/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|PNG|JPG|JPEG';
            $config['max_size'] = '100';
            $config['max_width']  = '150';
            $config['max_height']  = '180';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array[] = $data['file_name'];
                //$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
            }
            $names= implode(',', $name_array);
/*          $this->load->database();
            $db_data = array('id'=> NULL,
                             'name'=> $names);
        $this->db->insert('testtable',$db_data);
*/          print_r($names);
            //echo FCPATH;
            //$productname=$_POST["pname"];
        //  $description=$_POST["pdescription"];
        //  $price=$_POST["price"];
            //$catid=$_POST["catid"];

        //   $path = FCPATH.'img/product_uploads/original/';                        
        //  $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP","PNG");  
        //  if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {  
        //  $imagename = $_FILES['pimage']['name'];  
    //      $imagesize = $_FILES['pimage']['size'];  

        //  list($txt, $ext) = explode(".", $imagename);  
            //$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
              //print_r($products);
    //      $tmp = $_FILES['pimage']['tmp_name'];  
//if(move_uploaded_file($tmp, $path.$imagename)) {  
        //  $product_details=$this->category_model->getProductDetails();
            //print_r($product_details);
            //if(isset($products)){
                //echo "aa";
        //    $this->loadproduct();
             //}
        //  }  
            //else  
        //  {
            //echo "Image Upload Failed.";  
    //}
    }
public function logout()
    {
        $newdata = array(
        'adminuser_id'   =>'',
        'adminusername'  =>'',
        'adminemail'     => '',
        'logged_in' => FALSE,
        );
        $this->session->unset_userdata($newdata);
        $this->session->sess_destroy();
        $this->index();
    }

}





/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
4

3 回答 3

0

a)而不是写这个<input type="file" multiple="true" name="userfile" size="20" /><input type="file" name="userfile[]" size="20" />

b)在您的控制器内部,您可以执行以下操作:

$files = $_FILES;
$count = count($_FILES['userfile']['name']);
for($i=0; $i<$count; $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($config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();
}
于 2013-07-26T10:25:35.797 回答
0

你不能只添加一个multiple属性到<input type="file" />,那是行不通的。

您必须使用第三方模块/插件,例如Uploadify,才能运行此功能。

您还可以查看本教程,了解如何在 CodeIgniter 中集成 Uploadify(尽管我在您的代码中看不到任何 CodeIgniterish,但您仍然将问题标记为 CI 相关)

于 2013-07-26T09:56:06.877 回答
-1

使用它打开您的上传表单:

echo form_open_multipart('admin/do_upload');

放一个或多个后:

<input type="file" name="name" size="20" />

并使用这样的功能:

public function do_upload() {
        $config['upload_path'] = './assets/images/upload/';
        $config['allowed_types'] = 'gif|jpg|png';

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

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

            $this->load->view('admin/images', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());
        }
    }
于 2013-07-26T10:01:48.463 回答