使用codeigniter同时预览两个图像

时间:2017-06-19 04:42:48

标签: codeigniter

这是上传两个图像的控制器。第一个功能是do-upload,第二个功能是do_upload 2.

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Upload_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

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

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

    public function do_upload()
    {
        $config = array(
            'upload_path' => "./uploads/",
            'allowed_types' => "gif|jpg|png|jpeg|pdf",
            'overwrite' => TRUE,
            // Can be set to particular file size , here it is 2 MB(2048 Kb)
            'max_size' => "2048000",
            'max_height' => "768",
            'max_width' => "1024"
            );
        $this->load->library('upload', $config);
        if ($this->upload->do_upload()) {
            $data = array(
                'upload_data' => $this->upload->data()
                );
            $this->load->view('file_view', $data);
        } else {
            $error = array(
                'error' => $this->upload->display_errors()
                );
            $this->load->view('file_view', $error);
        }
    }

    public function do_upload2()
    {
        $config = array(
            'upload_path' => "./uploads/index2/",
            'allowed_types' => "gif|jpg|png|jpeg|pdf",
            'overwrite' => TRUE,
            // Can be set to particular file size , here it is 2 MB(2048 Kb)
            'max_size' => "2048000",
            'max_height' => "768",
            'max_width' => "1024"
            );
        $this->load->library('upload', $config);
        if ($this->upload->do_upload()) {
            $data = array(
                'upload_data' => $this->upload->data()
                );
            $this->load->view('file_view', $data);
        } else {
            $error = array(
                'error1' => $this->upload->display_errors()
                );
            $this->load->view('file_view', $error);
        }
    }
}

?>

这是file_view上传两个图像并查看两个图像。此代码的问题是,当我上传一个图像时,它只预览该图像。有人可以帮我在视图中同时预览这两个图像吗?

<?php echo form_open_multipart( 'upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>

<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
    <li></li>
    <img alt="Your uploaded image" src="<?=base_url(). 'uploads/' . $upload_data['file_name'];?>">
</ul>

<?php echo form_open_multipart( 'upload_controller/do_upload2');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>

<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
    <li></li>
    <img alt="Your uploaded image" src="<?=base_url(). 'uploads/index2/' . $upload_data['file_name'];?>">
</ul>
<p>
    <?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>

0 个答案:

没有答案