将图像从上传文件夹加载到模块HMVC codeginiter中

时间:2018-05-10 14:33:27

标签: codeigniter

我的HMVC应用程序中有下一棵树。

-application
   -modules
      -moduleOne
         -assets
         -controllers
         -models
         -views
         -uploads
            -images

我需要的是在视图中加载图像(来自上传/图像文件夹)。我怎样才能做到这一点? assets文件夹工作正常,但我需要将uploads文件夹分开。

我正在使用资产库: 在application / modules / moduleOne / controllers / Assets.php中我有:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

// include the global Assets class
include(APPPATH.'libraries/Assets.php');

在libraries / Assets.php中

 <?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Assets extends MX_Controller {

    function __construct() {
        parent::__construct();

    }

    function index(){
        //$this->user->authorize();

         if(count($this->uri->segments)==2){
             show_error("Serving assets for: ".APPPATH. 'modules/' . implode('/', $this->uri->segments));
             exit;
         }
        //---get working directory and map it to your module
        $file = APPPATH. 'modules/' . implode('/', $this->uri->segments);
        //----get path parts form extension
        $path_parts = pathinfo( $file);
        //---set the type for the headers
        $file_type=  strtolower($path_parts['extension']);

        if (is_file($file)) {
            //----write propper headers
            switch ($file_type) {
                case 'css':
                    header('Content-type: text/css');
                    break;

                case 'js':
                    header('Content-type: text/javascript');
                    break;

                case 'json':
                    header('Content-type: application/json;charset=UTF-8');
                    break;

                case 'xml':
                   header('Content-type: text/xml');
                    break;

                case 'pdf':
                  header('Content-type: application/pdf');
                    break;
                case 'svg':

                    header('Content-type: image/svg+xml;charset=UTF-8');
                    break;

                case 'jpg' || 'jpeg' || 'png' || 'gif':
                    header('Content-type: image/'.$file_type);
                    break;
            }

            readfile($file);
        } else {
            show_error("Asset not found: $file");
        }
        exit;
    }

}

它允许我在我的模块中使用资产。但是我需要在img src中将引用设置为uploads / images文件夹。

0 个答案:

没有答案