【问题标题】:Loop not looping fully on actioning on first iteration循环在第一次迭代时没有完全循环
【发布时间】:2010-01-15 21:27:33
【问题描述】:

我正在使用 codeigniter 中的 Multiple_upload 库进行多次上传,并形成一些创建缩略图的循环仅在第一次迭代时触发,这似乎是我的代码

function saveContentImages() {
    $this->load->model('categoryModel');
    if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){
        foreach($query as $k => $v) {
            $categoryTitle = strtolower($v['categoryTitle']);
        }
    }
    // we now need to set up the configuration that the upload
    // library expects to see.
    $config['upload_path'] = 'media/uploads/'.$categoryTitle;
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '1000';
    $sonfig['max_width'] = '1024';
    $config['max_height'] = '768';
    if(!file_exists($config['upload_path'])) {
        mkdir($config['upload_path'], 0777);
    }
    // load in both the libaries that the image upload we will
    // we import codeigniters own upload library and also a library
    // from the community that allows for multiple uploads with the help
    // of jQuery
    $this->load->library('upload', $config);
    $this->load->library('Multi_upload');
    // we can now do the multiple upload
    $files = $this->multi_upload->go_upload();
    echo $this->upload->display_errors();
    if(!$files) {
        $this->load->model('categoryModel');
        if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){
            foreach($query as $k => $v) {
                $categoryTitle = strtolower($v['categoryTitle']);
            }
        }
    } else {
        // we now need to do some GD library work so that the content can 
        // have thumbnail images
            foreach ($files as $image) {
            $gd['image_library'] = 'gd2';
            $gd['source_image'] = $image['file'];
            $gd['create_thumb'] = TRUE;
            $gd['maintain_ratio'] = TRUE;
            $gd['width'] = 63;
            $gd['height'] = 48;

            $this->load->library('image_lib', $gd);
            $resize = $this->image_lib->resize();
            echo $resize."<br />";
            // this condition gets run if the resize fails
            echo $this->image_lib->display_errors();
            if(!$resize) {
                echo $this->image_lib->display_errors();
            }
        }
        // loop through the $files array and save each image in the array
        foreach($files as $image) {
            $this->load->model('imageModel');
            $query = $this->imageModel->saveContentImages($this->input->post('contentTitle'), $image);
        }
        $data['formSubmitted'] = "Images";
        $this->load->view('admin/successPage', $data);
    }
}

当我 print_r 时,这是 $files 数组

( [0] => 数组 ( [名称] => 橙色.png [文件] => /Users/Simon/Sites/mysite/media/uploads/blog/orange.png [大小] => 3.07 [ext] => .png [image_type] => png [身高] => 703 [宽度] => 1000 )

[1] => Array
    (
        [name] => yellow.png
        [file] => /Users/Simon/Sites/mysite/media/uploads/blog/yellow.png
        [size] => 3.06
        [ext] => .png
        [image_type] => png
        [height] => 703
        [width] => 1000
    )

[2] => Array
    (
        [name] => purple.png
        [file] => /Users/Simon/Sites/mysite/media/uploads/blog/purple.png
        [size] => 3.07
        [ext] => .png
        [image_type] => png
        [height] => 703
        [width] => 1000
    )

)

有什么理由会这样做,或者我可以做任何检查吗?

【问题讨论】:

  • sico87,我不太确定问题出在哪里......文件没有上传还是只为第一个文件创建缩略图预览?

标签: php arrays codeigniter foreach multi-upload


【解决方案1】:

CodeIgniter 类加载器不会加载一个类的多个实例,除非每个实例都有不同的对象名称。因此,配置选项不会更新,因为 image_lib 构造函数没有运行。您应该在进入循环之前加载该类,然后手动对其进行初始化并在每次循环中清除它。

$gd = array(); $this->load->library('image_lib'); foreach ($files as $image) { $gd['image_library'] = 'gd2'; $gd['source_image'] = $image['file']; $gd['create_thumb'] = TRUE; $gd['maintain_ratio'] = TRUE; $gd['width'] = 63; $gd['height'] = 48; $this->image_lib->initialize($gd); $resize = $this->image_lib->resize(); $this->image_lib->clear(); }

【讨论】:

    最近更新 更多