2012-03-29 56 views
0

更新:完整控制器 下面的代码仅生成第一个图像的缩略图并成功上传所有其他图像。我面临的问题是,其他图像确实上传,但他们的缩略图不是。PHP/Codeigniter - For Loop

<?php 
class Upload extends CI_Controller{ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->upload_path = realpath(APPPATH . '../uploads'); 
    } 

    function index() 
    { 
     $this->load->view('upload_view', array('error' => '')); 
    } 

    function start() 
    { 
     // var_dump($_FILES); die(); 
     $config['upload_path'] = $this->upload_path; 
     $config['allowed_types'] = 'gif|jpg|jpeg|png'; 

     $this->load->library('upload', $config);    
     $status = $this->upload->do_multi_upload(); 
     for($i=0;$i<count($status);$i++) 
     { 
      $conf = array(
       'source_image' => $status[$i]['full_path'], 
       'new_image' => $this->upload_path . '/thumbs', 
       'maintain_ratio' => true, 
       'width' => 200, 
       'height' => 200 
      ); 

      $this->load->library('image_lib', $conf); 
      $this->image_lib->resize(); 
      $this->image_lib->clear(); 
     } 

     if (!$status)   
     { 
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('upload_view', $error); 
     }  
     else 
     { 
      var_dump($status); 
     } 
    } 
} 

?>

+0

这不是足够的代码。在我看来,目标图像被覆盖,因为你没有定义文件名。 – Straseus 2012-03-29 11:44:07

+0

可以看到更多的代码? – TheBlackBenzKid 2012-03-29 11:47:44

+0

啊!问题解决了。我所需要做的就是重新初始化$ conf变量。 – user1295342 2012-03-29 12:22:58

回答

0

负载之前开始循环,没有将参数传递给它的库。 然后,对每个在$状态的元素做到这一点:

$this->image_lib->initialize($conf); 

...你不需要使用clear()之后。

+0

未定义变量:conf – user1295342 2012-03-29 13:17:57

+0

我指的是您在描述中显示的$ conf数组。如果你已经删除/重命名它,或者如果你在定义它之前执行它 - 是的,它会给出一个“未定义的变量”错误。 :) – Narf 2012-03-29 15:57:22