我在视图codeigniter中显示pdf / doc / ppt图标但无法正常工作

时间:2016-10-13 12:35:26

标签: php codeigniter

我的观点upload_success.php

<html>
<head>
    <title>Upload Form</title>
    <style type="text/css">
        #gallery, #upload {
            border: 1px solid #ccc;
            margin: 10px auto;
            width: 570px;
            padding: 20px;
        }
        #blank_gallery{
            font-family: Arial;
            font-size: 18px;
            font-weight: bold;
            text-align: center;
        }
        .thumb{
            float: left;
            width: 150px;
            height: 100px;
            margin: 10px;
            padding: 10px;
            background-color: #ddd;

        }
        .thumb:hover{
            outline: 1px solid #999;
        }
        img{
            border: 0;
        }
        #gallery:after{
            content: ".";
            visibility: hidden;
            display: block;
            clear: both;
            height: 0;
            font-size: 0;
        }
    </style>
</head>
<body>
<div id="gallery">
    <?php  if(isset($images ) && count($images )):

        foreach ($images  as $image): ?>
            <div class = "thumb">
                <a href="<?php echo $image['full_path'];  ?>">
                    <img src="<?php echo $icon; ?>" width="150px" height="100px"/>
                </a>
            </div>
        <?php endforeach; else: ?>
        <div id ="blank_gallery"><?php echo @$error;?></div>
    <?php endif; ?>
</div>
<div id="upload">
    <?php
    echo form_open_multipart('upload/do_upload');
    echo form_upload('userfile');
    echo form_submit('upload', 'upload');
    echo form_close()
    ?>
</div>
</body>
</html>

controller upload.php

<?php

class Upload extends CI_Controller {
    var  $icon ;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        $this->load->view('upload_success', array('error' => ' ' ));
    }

    public function do_upload()
    {
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'doc|docx|pdf|ppt|pptx';
        $config['max_size']             = 0;
        $config['max_width']            = 3024;
        $config['max_height']           = 1768;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('userfile'))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_success', $error);
        }
        else {
            $this->load->library('upload', $config);
            $data = $this->upload->data();
            $filename = $this->upload->data('file_name');
            $filesize = $this->upload->data('file_size');
            $filetype = $this->upload->data('file_ext');
           // print_r($data);


            switch ($filetype) {
                case ".docx":
                    $icon = "\docss\images\word.png";
                    break;
                case ".pdf":
                    $icon = "\docss\images\pdf.jpg";
                    break;
                case ".pptx":
                    $icon = "\docss\images\ppt.png";
                    break;
                default:
                    echo "sorry there was an error uploading your file!!!";
            }


            $confyg = array(
                'source_image' => $data ['full_path'],
                'new_image' => './uploads/',
                'maintain_ration' => true,
                'width' => 150,
                'height' => 100
            );

            $this->load->model('upload_model');
            $data['images'] = $this->upload_model->getaimage();
            $data1 = array('icon' => $icon);
            $this->load->library('image_lib', $confyg);
            $this->image_lib->resize();
            $this->load->view('upload_success', $data1);
           $this->load->view('upload_success', $data);

        }
        }
    }

?>

model upload_model.php

    <?php
class Upload_model extends  CI_Model {
    function getaimage(){
        $files = scandir('./uploads/');
        $files = array_diff($files, array('.', '..' ));
        $images = array ();
        foreach ($files as $file){
            $images [] = array(
                'file_path' => base_url().'uploads/'.$file,
                'full_path' => base_url().'uploads/'.$file
            );
        }
        return $images;
    }

}

能够为每个分机显示正确的图标但是当我上传文件例如ppt文件时出现问题我的显示器中的每个其他文件(div)都采用该图标并且我有pdf / doc / ppt我的目录中的文件。我希望每个文件都根据其图标显示。切换条件工作正常,因为当我选择doc / pdf / ppt文件时,它会显示图标,但所有其他文件都会显示该图标。 我哪里出错?
在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

经过多天后,我找到了解决问题的方法,我只需要使用switch语句在数据库中插入相应的文件图标路径,然后从数据库中获取图标路径。

控制器中的

* * upload.php

    <?php

class Upload extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        $this->load->view('upload_success', array('error' => ' '));
    }

    public function do_upload()
    {

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'doc|docx|ppt|pptx|pdf|xls|xlsx';
        $config['max_size'] = 0;
        $config['max_width'] = 3024;
        $config['max_height'] = 1768;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_success', $error);
        } else {


            $data = array();

            $this->load->library('upload', $config);

            $fileData = $this->upload->data();
            $ext = $this->upload->data('file_ext');
            switch ($ext) {
                case ".docx":
                    $icon = "\docss\images\word.png";
                    break;
                case ".pdf":
                    $icon = "\docss\images\pdf.jpg";
                    break;
                case ".pptx":
                    $icon = "\docss\images\ppt.png";
                    break;
                default:
                    echo "sorry there was an error uploading your file!!!";
            }
            $uploadData['full_path'] = $fileData['full_path'];
            $uploadData['file_name'] = $fileData['file_name'];
            $uploadData['created'] = date("Y-m-d H:i:s");
            $uploadData['modified'] = date("Y-m-d H:i:s");
            $uploadData['file_icon'] = $icon; //the path of the approriate file icon to insert in db

        }



        if(!empty($uploadData)){
            //Insert file information into the database
           $this->db->insert('files',$uploadData);

            $confyg = array(
                'source_image' => $fileData ['full_path'],
                'new_image' => './uploads/thumbs/',
                'maintain_ration' => true,
                'width' => 150,
                'height' => 100
            );

            $this->load->library('image_lib', $confyg);
            $this->image_lib->resize();

            $this->load->model('upload_model');
            $data['icon'] = $this->upload_model->getaimage();

            $this->load->view('upload_success', $data);


        }
    }
}
?>

之后,代码按照我的意愿运行。 希望它能帮助另一个想要上传和显示适当文件图标的人。