我刚刚学习了codeigniter,我找到了一些上传和从文件夹中获取图片的教程,而且函数do_upload()可以工作并创建目录,但函数get_images()不起作用
这是我的模特:
var $gallery_path;
var $gallery_path_url;
function Gallery_model()
{
$this->gallery_path = realpath(APPPATH .'../images');
$this->gallery_path_url = base_url().'images/';
}
function get_images(){
$files = scandir($this->gallery_path.'thumbs');
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
);
}
}
控制器:
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload')){
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view("gallery", $data);
}
查看:
<div id="gallery">
<?php if(isset($images) && count($images)):
foreach ($images as $image):
?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</a>
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>