如果文件夹中没有图像,我正在处理一个应该说“请上传图像”的画廊,但旁边还显示上传的图像,但这些东西不能正常工作。
没有显示错误。
上传图像并将它们更改为缩略图工作并且它们进入正确的文件夹,但将它们拉出是另一回事,我检查了这些值并且它们进入了正确的目的地:
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
这是我的自动加载:
$autoload['helper'] = array('url', 'form', 'file');
这是我怀疑 $data 空出来的视图?:
<!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,
);
}
}
}
?>
非常感谢任何帮助!