【问题标题】:PHP/Codeigniter - For LoopPHP/Codeigniter - For 循环
【发布时间】:2012-03-29 11:31:10
【问题描述】:

更新:完整的控制器 下面的代码只生成第一张图片的缩略图并成功上传所有其他图片。我面临的问题是其他图片确实上传了,但他们的缩略图没有。

<?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);
        }
    }
}

?>

【问题讨论】:

  • 代码不够。在我看来,目标图像正在被覆盖,因为您没有定义文件名。
  • 啊!问题解决了。我需要做的就是重新初始化 $conf 变量。

标签: php codeigniter loops for-loop


【解决方案1】:

在开始循环之前加载库并且不向其传递参数。 然后,对 $status 中的每个元素执行以下操作:

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

...之后您不需要使用clear()

【讨论】:

  • 我指的是您在描述中显示的 $conf 数组。如果您已删除/重命名它,或者如果您在定义它之前执行此操作 - 是的,它会给出“未定义变量”错误。 :)
猜你喜欢
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2015-01-28
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多