2016-10-04 135 views
0

当我尝试上传一张图片,它不是在所有上传和我的代码是在这里文件上传被失败

public function image_upload($path) 
{ 

    if($_FILES) 
    { 

     $config['upload_path'] = './uploads/'.$path.'/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '5000'; 
     $config['max_width'] = '4000'; 
     $config['max_height'] = '6500'; 
     $config['file_name'] ='img'; 
     $this->load->library('upload', $config); 
     if(! $this->upload->do_upload("file")) 
     { 
      $this->session->set_flashdata('message',$this->upload->display_errors()); 
      return false; 
     } 
     else 
     { 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './uploads/'.$path.'/'.$this->upload->file_name; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = FALSE; 
      $config['width'] = 200; 
      $config['height'] = 150; 
      $config['new_image'] = './uploads/'.$path.'/thumb/'; 
      $this->load->library('image_lib',$config); 
      if(! $this->image_lib->resize()) 
      { 
       $this->session->set_flashdata('message',$this->image_lib->display_errors()); 
       return false; 
      } 
      else 
      { 
       return $this->upload->file_name; 
      } 
     } 
    } 
} 

我认为页是

<form role="form" action="<?php echo base_url();?>admin/add_staff" method="post" enctype="multipart/form-data"> 

      <tr> 
       <td>Photo</td> 
       <td><input type="file" id="file" name="file"></td>          
      </tr> 

      <tr> 
       <td></td> 
       <td> <input class="btn btn-primary save-btn" type="submit" value="Save" > </td> 
      </tr> 
    </form> 

我cntroller看起来像这样

public function add_staff() 
{ 
    $data['active_mn'] = 'add_staff'; 

    if($_POST) 
    { 

     $this->form_validation->set_rules('name', 'Name', 'required');     
     if($this->form_validation->run() == true) 
     { 
      if(! $this->image_upload($path = 'staff')) 
      { 
       $photo = ''; 
      } 
      else 
      { 
       $photo = $this->upload->file_name; 
      } 
      $data = array('photo'=>$photo); 
      $status = $this->admin_model->db_insert($table='staff',$data); 

      if($status) 
      { 
       $this->session->set_flashdata('message','Staff added Successfully'); 
      } 
      else 
      { 
       $this->session->set_flashdata('message','Insertion failed'); 
      } 

      redirect('admin/view_staff');    
     } 
    } 

    $this->load->view('admin/add_staff',$data); 
} 

我已经经历了几种方式,但没有得到解决这个问题。会是系统文件的问题吗?

+0

而你用来从$ _FILES ['file'] ['tmp_name'];'获取文件的代码在哪里?哪个是物理phile(双关语意思) – ArtisticPhoenix

+0

检查您试图上传的图像大小。在PHP默认是8M,如果你尝试上传超过这个限制,它不会上传。如果你想增加上传限制改变php.ini新的限制 – Veer

+0

不,我使用的图像大小只有不到8M –

回答

0

这里是我的代码上传多张图片

if(isset($_GET['did'])) 
{ 
    $id = $_GET['did']; 
    $part = "img/"; 
    $img = mysql_query("SELECT * FROM tbproduct_detail WHERE p_id = '$id'"); 
    while($r = mysql_fetch_object($img)) 
    { 
     $old = $r->profile; 
     unlink($part.$old); 

    } 
    mysql_query("DELETE FROM tbproduct_detail WHERE p_id = '$id'"); 
    mysql_query("DELETE FROM tbproduct WHERE id = '$id'"); 
} 


if(isset($_POST['upload'])) 
{ 
    $product_name = $_POST['product_name']; 

    mysql_query("INSERT INTO tbproduct(product_name) VALUES('$product_name')"); 
    $id = mysql_insert_id(); 
    if($id > 0) 
    { 
     foreach($_FILES['file']['tmp_name'] as $i => $tmp_name) 
     { 
      $filename = $_FILES['file']['name'][$i]; 
      $filetype = $_FILES['file']['type'][$i]; 
      $filesize = $_FILES['file']['size'][$i]; 
      $filetmp = $_FILES['file']['tmp_name'][$i]; 
      $store = rand(0,13248575858).$_FILES['file']['name'][$i]; 


      if(move_uploaded_file($filetmp,"img/".$store)) 
      { 
       mysql_query("INSERT INTO tbproduct_detail(p_id,file_name,file_size,file_ext,profile) VALUES('$id','$filename','$filesize','$filetype','$store')"); 
      } 

     } 
    } 


}?> 

这里观看部分

<form action="" method="post" enctype="multipart/form-data"> 
    <table class="table"> 
      <tr>      
       <td> 
        <label>ProductName</label> 
        <input type="text" name="product_name" class="form-control"> 
       </td> 

      </tr> 
      <tr> 
       <td> 
        <label>Profile</label> 
        <input type="file" name="file[]" multiple class="form-control">      
        </div> 
       </td> 

      </tr> 



      <tr>      
       <td> 
        <label>ProductName</label> 
        <input type="text" name="product_name" class="form-control"> 
       </td> 

      </tr> 
      <tr> 
       <td> 
        <label>Profile</label> 
        <input type="file" name="file[]" multiple class="form-control">      
        </div> 
       </td> 

      </tr> 
     <input type="submit" value="upload" class="btn btn-primary" name="upload"> 
    </table> 
</form> 

下面是SQL

CREATE TABLE `tbproduct_detail` (
 
`id` int(11) NOT NULL, 
 
    
 
`p_id` int(11) DEFAULT NULL, 
 

 
    `file_name` varchar(255) DEFAULT NULL, 
 
    
 
`file_size` varchar(255) DEFAULT NULL, 
 

 
    `file_ext` varchar(255) DEFAULT NULL, 
 
    
 
`profile` varchar(255) DEFAULT NULL 
 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT; 
 

 
CREATE TABLE `tbproduct` (
 
    `id` int(11) NOT NULL, 
 
    `product_name` varchar(255) DEFAULT NULL 
 
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

+0

我不需要上传多个图像 –

+0

你可以尝试甚至1图像,它没有问题:) @MOHAMMED –

+0

这不是codeigniter的代码 – Rijin