我在视图编码器中显示pdf/doc/ppt图标,但不能正常工作


i am displaying pdf/doc/ppt icons in view codeigniter but not working properly

my view 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>
h2控制器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);
        }
        }
    }
?>

模型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文件时,它显示图标,但所有其他文件都采用该图标。我在哪里出错了?
事先感谢您的协助。

许多天之后我找到了解决我的问题的方法,我只需要使用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);

        }
    }
}
?>

之后,代码就像我希望的那样工作了。希望它能帮助另一个人想要上传和显示适当的文件图标。