Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter图像库不能同时调整多个图像的大小_Codeigniter - Fatal编程技术网

Codeigniter图像库不能同时调整多个图像的大小

Codeigniter图像库不能同时调整多个图像的大小,codeigniter,Codeigniter,我试图在foreach循环中同时调整多个图像的大小,但没有这样做。它每页只调整其中一些的大小 问题:如何确保它在索引()上的foreach循环中同时调整多个图像的大小 正如您所见,它只调整了其中一些的大小,filemanager代码是在ajax中运行/加载的 这正是任何想要它的人的视野 <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="mo

我试图在foreach循环中同时调整多个图像的大小,但没有这样做。它每页只调整其中一些的大小

问题:如何确保它在索引()上的foreach循环中同时调整多个图像的大小

正如您所见,它只调整了其中一些的大小,filemanager代码是在ajax中运行/加载的

这正是任何想要它的人的视野

<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <div class="btn-toolbar mb-3" role="toolbar" aria-label="Toolbar with button groups">
            <div class="btn-group mr-2" role="group" aria-label="First group">
            <a class="btn btn-dark" href="<?php echo $parent;?>" data-toggle="tooltip" data-placement="top" title="<?php echo $button_parent; ?>" id="button-parent" class="btn btn-default"><i class="fa fa-level-up"></i></a> 
            <button type="button" data-toggle="tooltip" data-placement="top" title="<?php echo $button_upload; ?>" id="button-upload" class="btn btn-primary"><i class="fa fa-upload"></i></button>
            <button type="button" data-toggle="tooltip" data-placement="top" title="<?php echo $button_folder; ?>" id="button-folder" class="btn btn-dark"><i class="fa fa-folder"></i></button>
            <button type="button" data-toggle="tooltip" data-placement="top"  title="<?php echo $button_delete; ?>" id="button-delete" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>
            </div>

            <div class="input-group">
            <span class="input-group-btn">
            <button class="btn btn-secondary" type="button"><i class="fa fa-search" aria-hidden="true"></i></button>
            </span>
            <input type="text" class="form-control" placeholder="Search for..." aria-label="Search for...">
            </div>

            </div>
        </div>

        <div class="modal-header justify-content-center">
        <ol class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <li class="breadcrumb-item"><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
        <?php } ?>
        </ol>
        </div>

        <div class="modal-body">

        <!--
      <div class="row">
        <div class="col-sm-5">

            <a href="<?php echo $refresh;?>" data-toggle="tooltip" title="<?php echo $button_refresh; ?>" id="button-refresh" class="btn btn-default"><i class="fa fa-refresh"></i></a>



        </div>
      </div>

      -->

      <?php foreach (array_chunk($images, 4) as $image) { ?>
      <div class="row">
        <?php foreach ($image as $image) { ?>
        <div class="col-sm-3 text-center">
          <?php if ($image['type'] == 'directory') { ?>
          <div class="text-center"><a href="<?php echo $image['href']; ?>" class="directory" style="vertical-align: middle;"><i class="fa fa-folder fa-5x"></i></a></div>
          <label>
            <input type="checkbox" name="path[]" value="<?php echo $image['path']; ?>" />
            <?php echo $image['name']; ?></label>
          <?php } ?>
          <?php if ($image['type'] == 'image') { ?>
          <a href="<?php echo $image['href']; ?>" class="thumbnail">
          <?php echo $image['thumb']; ?></a>
          <label>
            <input type="hidden" name="cache[]" value="<?php echo $image['cache']; ?>" />
            <input type="checkbox" name="path[]" value="<?php echo $image['path']; ?>" />
            <?php echo $image['name']; ?></label>
          <?php } ?>
        </div>
        <?php } ?>
      </div>
      <?php } ?>
    </div>
    <div class="modal-footer  justify-content-center">
        <?php echo $pagination; ?>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('a.thumbnail').on('click', function(e) {
    e.preventDefault();

    <?php if ($thumb) { ?>
    $('#<?php echo $thumb; ?>').find('img').attr('src', $(this).find('img').attr('src'));
    <?php } ?>

    <?php if ($target) { ?>

    $('#<?php echo $target; ?>').attr('value', $(this).parent().find('input').attr('value'));
    <?php } else { ?>

    var range, sel = document.getSelection(); 

    if (sel.rangeCount) { 
        var img = document.createElement('img');
        img.src = $(this).attr('href');

        range = sel.getRangeAt(0); 
        range.insertNode(img); 
    }
    <?php } ?>

    $('#modal-image').modal('hide');
});

$('a.directory').on('click', function(e) {
    e.preventDefault();

    $('#modal-image').load($(this).attr('href'));
});

$('.pagination a').on('click', function(e) {
    e.preventDefault();

    $('#modal-image').load($(this).attr('href'));
});

$('.breadcrumb-item a').on('click', function(e) {
    e.preventDefault();

    $('#modal-image').load($(this).attr('href'));
});


$('#button-parent').on('click', function(e) {
    e.preventDefault();

    $('#modal-image').load($(this).attr('href'));
});

$('#button-refresh').on('click', function(e) {
    e.preventDefault();

    $('#modal-image').load($(this).attr('href'));
});

$('input[name=\'search\']').on('keydown', function(e) {
    if (e.which == 13) {
        $('#button-search').trigger('click');
    }
});

$('#button-search').on('click', function(e) {
    var url = 'admin/common/filemanager?&directory=<?php echo $directory; ?>';

    var filter_name = $('input[name=\'search\']').val();

    if (filter_name) {
        url += '&filter_name=' + encodeURIComponent(filter_name);
    }

    <?php if ($thumb) { ?>
    url += '&thumb=' + '<?php echo $thumb; ?>';
    <?php } ?>

    <?php if ($target) { ?>
    url += '?target=' + '<?php echo $target; ?>';
    <?php } ?>

    $('#modal-image').load(url);
});
//--></script> 
<script type="text/javascript"><!--
$('#button-upload').on('click', function() {
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" value="" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {
        if ($('#form-upload input[name=\'file\']').val() != '') {
            clearInterval(timer);

            $.ajax({
                url: '<?php echo base_url('admin/common/filemanager/upload?');?>directory=<?php echo $directory; ?>',
                type: 'post',       
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,     
                beforeSend: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
                    $('#button-upload').prop('disabled', true);
                },
                complete: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-upload"></i>');
                    $('#button-upload').prop('disabled', false);
                },
                success: function(json) {
                    if (json['error']) {
                        alert(json['error']);
                    }

                    if (json['success']) {
                        alert(json['success']);

                        $('#button-refresh').trigger('click');
                    }
                },          
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }); 
        }
    }, 500);
});

$('#button-folder').popover({
    html: true,
    placement: 'bottom',
    trigger: 'click',
    title: 'New Folder',
    content: function() {
        html  = '<div class="input-group">';
        html += '  <input type="text" name="folder" value="" placeholder="New Folder" class="form-control">';
        html += '  <span class="input-group-btn"><button type="button" title="" id="button-create" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></span>';
        html += '</div>';

        return html;    
    }
});

$('#button-folder').on('shown.bs.popover', function() {
    $('#button-create').on('click', function() {
        $.ajax({
            url: 'admin/common/filemanager/folder?directory=<?php echo $directory; ?>',
            type: 'post',       
            dataType: 'json',
            data: 'folder=' + encodeURIComponent($('input[name=\'folder\']').val()),
            beforeSend: function() {
                $('#button-create').prop('disabled', true);
            },
            complete: function() {
                $('#button-create').prop('disabled', false);
            },
            success: function(json) {
                if (json['error']) {
                    alert(json['error']);
                }

                if (json['success']) {
                    alert(json['success']);

                    $('#button-refresh').trigger('click');
                }
            },          
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }); 
});

$('#modal-image #button-delete').on('click', function(e) {
    if (confirm('Are You Sure')) {
        $.ajax({
            url: 'admin/common/filemanager/delete/',
            type: 'post',       
            dataType: 'json',
            data: {
                path: $('input[name^=\'path\']:checked'),
                cache: $('input[name^=\'cache\']').val() 
            },
            beforeSend: function() {
                $('#button-delete').prop('disabled', true);
            },  
            complete: function() {
                $('#button-delete').prop('disabled', false);
            },      
            success: function(json) {
                if (json['error']) {
                    alert(json['error']);
                }

                if (json['success']) {
                    alert(json['success']);

                    $('#button-refresh').trigger('click');
                }
            },          
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
});
//--></script>

$('#').find('img').attr('src',$(this.find('img').attr('src'));
$('#').attr('value',$(this.parent().find('input').attr('value'));
变量范围,sel=document.getSelection();
如果(选择范围计数){
var img=document.createElement('img');
img.src=$(this.attr('href');
范围=选择范围(0);
range.insertNode(img);
}
$(“#模态图像”).modal('hide');
});
$('a.directory')。在('click',函数(e){
e、 预防默认值();
$('#模态图像').load($(this.attr('href'));
});
$('.pagination a')。在('click',函数(e)上{
e、 预防默认值();
$('#模态图像').load($(this.attr('href'));
});
$('.breadcrumb item a')。在('click',函数(e)上{
e、 预防默认值();
$('#模态图像').load($(this.attr('href'));
});
$(“#按钮父项”)。在('click',函数(e)上{
e、 预防默认值();
$('#模态图像').load($(this.attr('href'));
});
$(“#按钮刷新”)。在('click',函数(e){
e、 预防默认值();
$('#模态图像').load($(this.attr('href'));
});
$('input[name=\'search\']')。在('keydown',函数(e)上{
如果(e.which==13){
$(“#按钮搜索”).trigger('click');
}
});
$(“#按钮搜索”)。在('click',函数(e)上{
var url='admin/common/filemanager?&directory=';
var filter_name=$('input[name=\'search\']')。val();
if(过滤器名称){
url+='&过滤器名称='+编码器组件(过滤器名称);
}
url+='&thumb='+'';
url+='?目标='+';
$(“#模态图像”).load(url);
});
//--> 

尝试在resize()上添加错误处理程序,以查看是否存在阻止图像调整大小的错误:

$this->image_lib->clear();        
$this->image_lib->initialize($config);

if (!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
}
更新

该段似乎没有正确初始化

    $segment = $this->input->get('per_page');
相反,尝试

    $segment = $this->input->get('page') * $this->input->get('per_page');
解决方案 我现在有了有效的解决方案

我必须检查一下目录

if (!is_dir(DIR_IMAGE . 'cache/' . $new_image)) {
      if ($this->input->get('directory')) {
         @mkdir(DIR_IMAGE . 'cache/catalog/' . $this->input->get('directory') .'/', 0777, true);
      } else {
         @mkdir(DIR_IMAGE . 'cache/catalog/', 0777, true);
      }
}
还可以使用if文件进行调整大小检查,还可以使用codeigniter img()helper函数

全控器

<?php

class Filemanager extends MX_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->library('image_lib');
        $this->load->helper('html');
        define('DIR_IMAGE', FCPATH . 'image/');
        
    }

    public function index()
    {
        $data['resize_error'] ='';

        $input_get_directory = $this->input->get('directory');
        $input_get_page = $this->input->get('page');
        $input_get_filter = $this->input->get('filter_name');
        $input_get_target = $this->input->get('target');
        $input_get_thumb = $this->input->get('thumb');

        if (isset($input_get_filter)) {
            $filter_name = $input_get_filter;
        } else {
            $filter_name = null;
        }

        // Make sure we have the correct directory
        if (isset($input_get_directory)) {

            $directory = FCPATH . 'image/catalog/' . $input_get_directory;

        } else {

            // Do not add extra tralier slash at end /
            $directory = FCPATH . 'image/catalog';
        }

        if (isset($input_get_page)) {
            $page = $input_get_page;
        } else {
            $page = 1;
        }

        $data['images'] = array();

        // Get directories
        $directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);

        if (!$directories) {
            $directories = array();
        }

        // Get files
        $files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);

        if (!$files) {
            $files = array();
        }

        // Merge directories and files
        $images = array_merge($directories, $files);

        // Get total number of files and directories
        $image_total = count($images);

        $per_page = 8;
        $segment = $this->input->get('per_page');
        $segment += $per_page;

        $this->load->model('admin/tool/image_model');
        $this->load->helper('string');

        foreach ($images as $key => $image) {

            if ($key < $segment && $key >= $segment - $per_page) {

                $name = basename(preg_replace("/\.[^.]+$/", "", $image));

                if (is_dir($image)) {

                    $url = '';

                    if (isset($input_get_target)) {
                        $url .= '&target=' . $input_get_target;
                    }

                    if (isset($input_get_thumb)) {
                        $url .= '&thumb=' . $input_get_thumb;
                    }

                    $data['images'][] = array(
                        'thumb' => '',
                        'name' => $name,
                        'href' => '',
                        'type' => 'directory',
                        'path' => substr($image, strlen(FCPATH . 'image/')),
                        'href' => site_url('admin/common/filemanager/?&directory=' . substr($image, strlen(FCPATH . 'image/' . 'catalog/')) . $url)
                    );

                } elseif (is_file($image)) {

                $width = 100;
                $height = 100;

                $old_filename = substr($image, strlen(DIR_IMAGE));

                $extension = pathinfo($old_filename, PATHINFO_EXTENSION);

                $new_image = substr($old_filename, 0, strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

                if (!is_dir(DIR_IMAGE . 'cache/' . $new_image)) {
                    if ($this->input->get('directory')) {
                        @mkdir(DIR_IMAGE . 'cache/catalog/' . $this->input->get('directory') .'/', 0777, true);
                    } else {
                        @mkdir(DIR_IMAGE . 'cache/catalog/', 0777, true);
                    }
                }

                if (!file_exists(DIR_IMAGE . 'cache/' . $new_image)) {

                    $config = array(
                        'image_library' => 'gd2',
                        'source_image' => $image,
                        'create_thumb' => false,
                        'maintain_ratio' => false,
                        'width' => $width,
                        'height' => $height,
                        'overwrite' => true,
                        'new_image' => DIR_IMAGE . 'cache/' . $new_image
                    );

                    $this->image_lib->initialize($config);  
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }

                $data['images'][] = array(
                        'type' => 'image',
                        'href' => '',
                        'thumb' => img('image/cache/'. $new_image),
                        'name' => (strlen($name) > 13) ? substr($name,0,10).'...' : $name,
                        'path' => substr($image, strlen(DIR_IMAGE))
                    );
                }
            }
        }

        $data['heading_title'] = "Image Manager";

        $data['text_no_results'] = "No Results";
        $data['text_confirm'] = "Are You Sure";

        $data['entry_search'] = "Search..";
        $data['entry_folder'] = "New Folder";

        $data['button_parent'] = "Parent";
        $data['button_refresh'] = "Refresh";
        $data['button_upload'] = "Upload";
        $data['button_folder'] = "New Folder";
        $data['button_delete'] = "Delete";
        $data['button_search'] = "Search";

        if (isset($input_get_directory)) {
            $data['directory'] = $input_get_directory;
        } else {
            $data['directory'] = '';
        }

        // Return the filter name
        if (isset($input_get_filter)) {
            $data['filter_name'] = $input_get_filter;
        } else {
            $data['filter_name'] = '';
        }

        // Return the target ID for the file manager to set the value
        if (isset($input_get_target)) {
            $data['target'] = $input_get_target;
        } else {
            $data['target'] = '';
        }

        // Return the thumbnail for the file manager to show a thumbnail
        if (isset($input_get_thumb)) {
            $data['thumb'] = $input_get_thumb;
        } else {
            $data['thumb'] = '';
        }

        // Parent
        $url = '';

        if (isset($input_get_directory)) {
            $pos = strrpos($input_get_directory, '/');

            if ($pos) {
                $url .= '/?&directory=' . substr($input_get_directory, 0, $pos);
            }
        }

        $data['parent'] = site_url('admin/common/filemanager' . $url);

        // Refresh
        $url = '';

        if (isset($input_get_directory)) {
            $url .= '/?&directory=' . $input_get_directory;
        }

        if (isset($input_get_target)) {
            $url .= '&target=' . $input_get_target;
        }

        if (isset($input_get_thumb)) {
            $url .= '&thumb=' . $input_get_thumb;
        }

        $data['refresh'] = site_url('admin/common/filemanager' . $url);

        $url = '';

        if (isset($input_get_directory)) {
            $url .= '?&directory=' . $input_get_directory;
        }

        if (isset($input_get_filter)) {
            $url .= '&filter_name=' . $input_get_filter;
        }

        if (isset($input_get_target)) {
            $url .= '/?&target=' . $input_get_target;
        }

        if (isset($input_get_thumb)) {
            $url .= '&thumb=' . $input_get_thumb;
        }

        $this->load->library('pagination');

        $config['base_url'] = base_url('admin/common/filemanager' . $url);
        $config['total_rows'] = $image_total;
        $config['per_page'] = $per_page;
        $config['page_query_string'] = TRUE;
        $config['num_links'] = "16";
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";

        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        $this->load->view('common/filemanager_view', $data);
    }

    public function resize($filename) {
        $this->load->library('image_lib');

       //foreach ($filename as $key => $file) {}

        $width = 100;
        $height = 100;

        $old_filename = substr($filename, strlen(FCPATH . 'image/'));

        $extension = pathinfo($old_filename, PATHINFO_EXTENSION);

        $new_image = substr($old_filename, 0, strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

        $config['image_library'] = 'gd2';
        $config['source_image'] = $filename;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['new_image'] = FCPATH . 'image/cache/' . $new_image;
         
        $this->image_lib->clear();        
        $this->image_lib->initialize($config);
        
        
        if (!$this->image_lib->resize()) {
            return $this->image_lib->display_errors();
        } else {
            return base_url('image/cache/') . $new_image;
        }
    }

    /**
     * Codeigniter upload library with json and config_item
     */
    public function upload()
    {
        $json = array();

        $input_get_directory = $this->input->get('directory');

        if (isset($input_get_directory)) {
            $directory = 'image/catalog/' . $input_get_directory;
        } else {
            $directory = 'image/catalog';
        }

        $config['upload_path'] = './' . $directory . '/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 5000;
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['overwrite'] = FALSE;

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

        $input_name = "file";

        if ($this->upload->do_upload($input_name) == FALSE) {
            $json['error'] = $this->upload->display_errors();
        } else {
            $file_data = $this->upload->data();
            $json['success'] = "This file" . ' ' . $file_data['file_name'] . ' ' . "has been uploaded";
        }

        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }

    public function folder()
    {
        $json = array();

        $input_get_directory = $this->input->get('directory');

        // Make sure we have the correct directory
        if (isset($input_get_directory)) {

            $directory = FCPATH . 'image/catalog/' . $input_get_directory;

        } else {

            // Do not add extra tralier slash at end /
            $directory = FCPATH . 'image/catalog';
        }

        // Check its a directory
        if (!is_dir($directory)) {
            $json['error'] = $this->lang->line('error_directory');
        }

        if (!$json) {

            $input_get_folder = $this->input->post('folder');

            // Sanitize the folder name
            $folder = str_replace(array('../', '..\\', '..'), '', basename(html_entity_decode($input_get_folder, ENT_QUOTES, 'UTF-8')));

            // Validate the filename length
            if ((utf8_strlen($folder) < 3) || (utf8_strlen($folder) > 128)) {
                $json['error'] = $this->lang->line('error_folder');
            }

            // Check if directory already exists or not
            if (is_dir($directory . '/' . $folder)) {
                $json['error'] = $this->lang->line('error_exists');
            }
        }

        if (!$json) {
            mkdir($directory . '/' . $folder, 0777);

            $json['success'] = $this->lang->line('text_directory');
        }


        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }

    public function delete()
    {
        $json = array();

        $input_path_post = $this->input->post('path');

        if (isset($input_path_post)) {
            $paths = $input_path_post;
        } else {
            $paths = array();
        }

        // Loop through each path to run validations
        foreach ($paths as $path) {
            $path = rtrim(FCPATH . 'image/' . str_replace(array('../', '..\\', '..'), '', $path), '/');

            // Check path exsists
            if ($path == FCPATH . 'image/' . 'catalog') {
                $json['error'] = $this->lang->line('error_delete');

                break;
            }
        }

        if (!$json) {
            // Loop through each path
            foreach ($paths as $path) {
                $path = rtrim(FCPATH . 'image/' . str_replace(array('../', '..\\', '..'), '', $path), '/');

                // If path is just a file delete it
                if (is_file($path)) {
                    unlink($path);

                    // If path is a directory beging deleting each file and sub folder
                } elseif (is_dir($path)) {
                    $files = array();

                    // Make path into an array
                    $path = array($path . '*');

                    // While the path array is still populated keep looping through
                    while (count($path) != 0) {
                        $next = array_shift($path);

                        foreach (glob($next) as $file) {
                            // If directory add to path array
                            if (is_dir($file)) {
                                $path[] = $file . '/*';
                            }

                            // Add the file to the files to be deleted array
                            $files[] = $file;
                        }
                    }

                    // Reverse sort the file array
                    rsort($files);

                    foreach ($files as $file) {
                        // If file just delete
                        if (is_file($file)) {
                            unlink($file);

                            // If directory use the remove directory function
                        } elseif (is_dir($file)) {
                            rmdir($file);
                        }
                    }
                }
            }

            $json['success'] = $this->lang->line('text_delete');
        }

        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }
}

我通常使用(隐藏=不在视口中)html5画布来调整图像的大小客户端,您可以保存大量资源。没有任何更改仍然相同。@如果将
echo
替换为
exit
,它会停止脚本吗?我想知道
echo
的输出是否不可见?仍然不工作。当我在foreach循环中使用它时,它似乎跳过了一些图像。当我点击一个按钮时,文件管理器弹出一个窗口modal@wolfgang1983我怀疑问题可能在这里
($key<$segment&&$key>=$segment-$per_page)
-这个
$this->input->get('per_page')的值是多少?它现在可以调整它们的大小,但图像仍然不会显示在第一批。这是非常错误的:
if($This->input->get('directory')){
永远不要相信用户的输入。有人可能会编写脚本,使成千上万的目录变为空,并阻止您的服务器。您不能允许用户具有这种能力。@Tpojka举个例子,说明您认为最好的方法,然后最好的方法是不允许用户具有类似
https://example.com/filemanager/upload?directory=what_ever_i_i_想要什么
这是手动构建的入门,但恶意代码可能会从外部服务器发出cURL请求,并用垃圾目录填充您的服务器。您需要提供一些额外的安全性来禁止这种情况。您必须非常小心谁和何时能够使用此方法。除此之外,我不知道您的专业人士JET/业务逻辑告诉更多。只是提供2c建议您应该注意什么。我没有检查问题,只是看到了这个安全漏洞。我不理解curl@Tpojka改用codeigniter uri段怎么样?
if (!is_dir(DIR_IMAGE . 'cache/' . $new_image)) {
      if ($this->input->get('directory')) {
         @mkdir(DIR_IMAGE . 'cache/catalog/' . $this->input->get('directory') .'/', 0777, true);
      } else {
         @mkdir(DIR_IMAGE . 'cache/catalog/', 0777, true);
      }
}
<?php

class Filemanager extends MX_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->library('image_lib');
        $this->load->helper('html');
        define('DIR_IMAGE', FCPATH . 'image/');
        
    }

    public function index()
    {
        $data['resize_error'] ='';

        $input_get_directory = $this->input->get('directory');
        $input_get_page = $this->input->get('page');
        $input_get_filter = $this->input->get('filter_name');
        $input_get_target = $this->input->get('target');
        $input_get_thumb = $this->input->get('thumb');

        if (isset($input_get_filter)) {
            $filter_name = $input_get_filter;
        } else {
            $filter_name = null;
        }

        // Make sure we have the correct directory
        if (isset($input_get_directory)) {

            $directory = FCPATH . 'image/catalog/' . $input_get_directory;

        } else {

            // Do not add extra tralier slash at end /
            $directory = FCPATH . 'image/catalog';
        }

        if (isset($input_get_page)) {
            $page = $input_get_page;
        } else {
            $page = 1;
        }

        $data['images'] = array();

        // Get directories
        $directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);

        if (!$directories) {
            $directories = array();
        }

        // Get files
        $files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);

        if (!$files) {
            $files = array();
        }

        // Merge directories and files
        $images = array_merge($directories, $files);

        // Get total number of files and directories
        $image_total = count($images);

        $per_page = 8;
        $segment = $this->input->get('per_page');
        $segment += $per_page;

        $this->load->model('admin/tool/image_model');
        $this->load->helper('string');

        foreach ($images as $key => $image) {

            if ($key < $segment && $key >= $segment - $per_page) {

                $name = basename(preg_replace("/\.[^.]+$/", "", $image));

                if (is_dir($image)) {

                    $url = '';

                    if (isset($input_get_target)) {
                        $url .= '&target=' . $input_get_target;
                    }

                    if (isset($input_get_thumb)) {
                        $url .= '&thumb=' . $input_get_thumb;
                    }

                    $data['images'][] = array(
                        'thumb' => '',
                        'name' => $name,
                        'href' => '',
                        'type' => 'directory',
                        'path' => substr($image, strlen(FCPATH . 'image/')),
                        'href' => site_url('admin/common/filemanager/?&directory=' . substr($image, strlen(FCPATH . 'image/' . 'catalog/')) . $url)
                    );

                } elseif (is_file($image)) {

                $width = 100;
                $height = 100;

                $old_filename = substr($image, strlen(DIR_IMAGE));

                $extension = pathinfo($old_filename, PATHINFO_EXTENSION);

                $new_image = substr($old_filename, 0, strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

                if (!is_dir(DIR_IMAGE . 'cache/' . $new_image)) {
                    if ($this->input->get('directory')) {
                        @mkdir(DIR_IMAGE . 'cache/catalog/' . $this->input->get('directory') .'/', 0777, true);
                    } else {
                        @mkdir(DIR_IMAGE . 'cache/catalog/', 0777, true);
                    }
                }

                if (!file_exists(DIR_IMAGE . 'cache/' . $new_image)) {

                    $config = array(
                        'image_library' => 'gd2',
                        'source_image' => $image,
                        'create_thumb' => false,
                        'maintain_ratio' => false,
                        'width' => $width,
                        'height' => $height,
                        'overwrite' => true,
                        'new_image' => DIR_IMAGE . 'cache/' . $new_image
                    );

                    $this->image_lib->initialize($config);  
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }

                $data['images'][] = array(
                        'type' => 'image',
                        'href' => '',
                        'thumb' => img('image/cache/'. $new_image),
                        'name' => (strlen($name) > 13) ? substr($name,0,10).'...' : $name,
                        'path' => substr($image, strlen(DIR_IMAGE))
                    );
                }
            }
        }

        $data['heading_title'] = "Image Manager";

        $data['text_no_results'] = "No Results";
        $data['text_confirm'] = "Are You Sure";

        $data['entry_search'] = "Search..";
        $data['entry_folder'] = "New Folder";

        $data['button_parent'] = "Parent";
        $data['button_refresh'] = "Refresh";
        $data['button_upload'] = "Upload";
        $data['button_folder'] = "New Folder";
        $data['button_delete'] = "Delete";
        $data['button_search'] = "Search";

        if (isset($input_get_directory)) {
            $data['directory'] = $input_get_directory;
        } else {
            $data['directory'] = '';
        }

        // Return the filter name
        if (isset($input_get_filter)) {
            $data['filter_name'] = $input_get_filter;
        } else {
            $data['filter_name'] = '';
        }

        // Return the target ID for the file manager to set the value
        if (isset($input_get_target)) {
            $data['target'] = $input_get_target;
        } else {
            $data['target'] = '';
        }

        // Return the thumbnail for the file manager to show a thumbnail
        if (isset($input_get_thumb)) {
            $data['thumb'] = $input_get_thumb;
        } else {
            $data['thumb'] = '';
        }

        // Parent
        $url = '';

        if (isset($input_get_directory)) {
            $pos = strrpos($input_get_directory, '/');

            if ($pos) {
                $url .= '/?&directory=' . substr($input_get_directory, 0, $pos);
            }
        }

        $data['parent'] = site_url('admin/common/filemanager' . $url);

        // Refresh
        $url = '';

        if (isset($input_get_directory)) {
            $url .= '/?&directory=' . $input_get_directory;
        }

        if (isset($input_get_target)) {
            $url .= '&target=' . $input_get_target;
        }

        if (isset($input_get_thumb)) {
            $url .= '&thumb=' . $input_get_thumb;
        }

        $data['refresh'] = site_url('admin/common/filemanager' . $url);

        $url = '';

        if (isset($input_get_directory)) {
            $url .= '?&directory=' . $input_get_directory;
        }

        if (isset($input_get_filter)) {
            $url .= '&filter_name=' . $input_get_filter;
        }

        if (isset($input_get_target)) {
            $url .= '/?&target=' . $input_get_target;
        }

        if (isset($input_get_thumb)) {
            $url .= '&thumb=' . $input_get_thumb;
        }

        $this->load->library('pagination');

        $config['base_url'] = base_url('admin/common/filemanager' . $url);
        $config['total_rows'] = $image_total;
        $config['per_page'] = $per_page;
        $config['page_query_string'] = TRUE;
        $config['num_links'] = "16";
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";

        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        $this->load->view('common/filemanager_view', $data);
    }

    public function resize($filename) {
        $this->load->library('image_lib');

       //foreach ($filename as $key => $file) {}

        $width = 100;
        $height = 100;

        $old_filename = substr($filename, strlen(FCPATH . 'image/'));

        $extension = pathinfo($old_filename, PATHINFO_EXTENSION);

        $new_image = substr($old_filename, 0, strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

        $config['image_library'] = 'gd2';
        $config['source_image'] = $filename;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['new_image'] = FCPATH . 'image/cache/' . $new_image;
         
        $this->image_lib->clear();        
        $this->image_lib->initialize($config);
        
        
        if (!$this->image_lib->resize()) {
            return $this->image_lib->display_errors();
        } else {
            return base_url('image/cache/') . $new_image;
        }
    }

    /**
     * Codeigniter upload library with json and config_item
     */
    public function upload()
    {
        $json = array();

        $input_get_directory = $this->input->get('directory');

        if (isset($input_get_directory)) {
            $directory = 'image/catalog/' . $input_get_directory;
        } else {
            $directory = 'image/catalog';
        }

        $config['upload_path'] = './' . $directory . '/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 5000;
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['overwrite'] = FALSE;

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

        $input_name = "file";

        if ($this->upload->do_upload($input_name) == FALSE) {
            $json['error'] = $this->upload->display_errors();
        } else {
            $file_data = $this->upload->data();
            $json['success'] = "This file" . ' ' . $file_data['file_name'] . ' ' . "has been uploaded";
        }

        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }

    public function folder()
    {
        $json = array();

        $input_get_directory = $this->input->get('directory');

        // Make sure we have the correct directory
        if (isset($input_get_directory)) {

            $directory = FCPATH . 'image/catalog/' . $input_get_directory;

        } else {

            // Do not add extra tralier slash at end /
            $directory = FCPATH . 'image/catalog';
        }

        // Check its a directory
        if (!is_dir($directory)) {
            $json['error'] = $this->lang->line('error_directory');
        }

        if (!$json) {

            $input_get_folder = $this->input->post('folder');

            // Sanitize the folder name
            $folder = str_replace(array('../', '..\\', '..'), '', basename(html_entity_decode($input_get_folder, ENT_QUOTES, 'UTF-8')));

            // Validate the filename length
            if ((utf8_strlen($folder) < 3) || (utf8_strlen($folder) > 128)) {
                $json['error'] = $this->lang->line('error_folder');
            }

            // Check if directory already exists or not
            if (is_dir($directory . '/' . $folder)) {
                $json['error'] = $this->lang->line('error_exists');
            }
        }

        if (!$json) {
            mkdir($directory . '/' . $folder, 0777);

            $json['success'] = $this->lang->line('text_directory');
        }


        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }

    public function delete()
    {
        $json = array();

        $input_path_post = $this->input->post('path');

        if (isset($input_path_post)) {
            $paths = $input_path_post;
        } else {
            $paths = array();
        }

        // Loop through each path to run validations
        foreach ($paths as $path) {
            $path = rtrim(FCPATH . 'image/' . str_replace(array('../', '..\\', '..'), '', $path), '/');

            // Check path exsists
            if ($path == FCPATH . 'image/' . 'catalog') {
                $json['error'] = $this->lang->line('error_delete');

                break;
            }
        }

        if (!$json) {
            // Loop through each path
            foreach ($paths as $path) {
                $path = rtrim(FCPATH . 'image/' . str_replace(array('../', '..\\', '..'), '', $path), '/');

                // If path is just a file delete it
                if (is_file($path)) {
                    unlink($path);

                    // If path is a directory beging deleting each file and sub folder
                } elseif (is_dir($path)) {
                    $files = array();

                    // Make path into an array
                    $path = array($path . '*');

                    // While the path array is still populated keep looping through
                    while (count($path) != 0) {
                        $next = array_shift($path);

                        foreach (glob($next) as $file) {
                            // If directory add to path array
                            if (is_dir($file)) {
                                $path[] = $file . '/*';
                            }

                            // Add the file to the files to be deleted array
                            $files[] = $file;
                        }
                    }

                    // Reverse sort the file array
                    rsort($files);

                    foreach ($files as $file) {
                        // If file just delete
                        if (is_file($file)) {
                            unlink($file);

                            // If directory use the remove directory function
                        } elseif (is_dir($file)) {
                            rmdir($file);
                        }
                    }
                }
            }

            $json['success'] = $this->lang->line('text_delete');
        }

        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));
    }
}