CodeIgniter 并显示图像/上传图像

问题描述 投票:0回答:1

我正在开发一个画廊,如果文件夹中没有图像,应该会显示“请上传图像”,但旁边还显示已上传的图像,但这些东西无法正常工作。

没有显示任何错误。

上传图像并将其更改为缩略图可以工作,它们会进入正确的文件夹,但将它们拉出来是另一回事,我已经检查了这些值,它们会转到正确的目的地:

    $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,
        );

    }

}

}

?>

非常感谢任何帮助!

php image codeigniter
1个回答
0
投票
function get_images(){
    /* all the stuff you already have... */

    /* return the array! */
    return $images;
}
© www.soinside.com 2019 - 2024. All rights reserved.