codeigniter上传图片代码网址错误

时间:2013-01-28 10:30:44

标签: image codeigniter upload codeigniter-2

这是关于来自

的codeigniter图像上传和图像处理第9天

http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-file-uploading-and-image-manipulation/

我认为此代码与最新的CI不兼容。

我收到错误

“在此服务器上找不到请求的URL / codeigniter / gallery。”

我的htaccess设置为删除index.php 我已将我的基本网址设置为localhost / codeigniter

模型

<?php
class Gallery_model extends CI_Model {

var $gallery_path;
var $gallery_path_url;

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

    $this->gallery_path = realpath(APPPATH . '../images');
    $this->gallery_path_url = base_url().'images';
}

function do_upload() {

    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width' => 150,
        'height' => 100
    );

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

}

function get_images() {

    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.', '..', 'thumbs'));

    $images = array();

    foreach ($files as $file) {
        $images []= array (
            'url' => $this->gallery_path_url . $file,
            'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
        );
    }

    return $images;
}

}

控制器

<?php
class Gallery extends CI_Controller {

function index() {

    $this->load->model('Gallery_model');

    if ($this->input->post('upload')) {
        $this->Gallery_model->do_upload();
    }

    $data['images'] = $this->Gallery_model->get_images();

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

}

}    

1 个答案:

答案 0 :(得分:0)

你尝试过绝对网址吗?

进行测试,而不是base_url()使用enteire路径"http://mywebsite.com/path/to/gallery"

如果它与绝对网址一起使用,则表示您正在使用worng网址路径,然后您可以修复它