0
我正在一个画廊上工作,如果文件夹中没有图像,应该说“请上传图像”,但旁边也显示上传的图像,但这些东西都不起作用正确。CodeIgniter和显示图像/上传图像
没有错误显示。
上传图片,并改变他们进入缩略图工作,他们在正确的文件夹中去,但是拉出来是一个不同的故事,我已经检查了这些价值观和他们去到正确的目的地:
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
和这是我的自动加载:
$autoload['helper'] = array('url', 'form', 'file');
这就是我怀疑$数据视图是走出来的空?:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Gallery</title>
</head>
<body>
<div id="gallery">
<?php if (isset($data) && count($data)) : ;?>
<?php foreach($images as $image): ?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</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>
</body>
</html>
这是我的控制器:
<?php
class Gallery extends CI_Controller
{
function index()
{
$this->load->model('Gallery_model');
if($this->input->post('upload'))
{
//handle upload
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery', $data);
}
}
?>
这是模型:
<?php
class Gallery_model extends CI_Model{
var $gallery_path;
var $gallery_path_url;
function __construct()
{
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
}
function do_upload()
{
//handle userfile
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
die($this->upload->display_errors());
}
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize())
{
die($this->image_lib->display_errors());
}
}
function get_images(){
$files = scandir($this->gallery_path);
$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,
);
}
}
}
?>
任何帮助,非常感谢!
您是否告诉他将模型中的所有内容移动到函数get_images中?在控制器中拥有这些功能不是更好吗? – 2012-08-10 13:20:23
我不评论他的代码质量。我指出,get_images()函数需要返回一些东西。 – Mudshark 2012-08-14 10:29:41