Welcome Guest, Not a member yet? Register   Sign In
base_url(), site_url(), nothing working to display images
#1

[eluser]nlweb[/eluser]
I am following some tutorials on an image gallery, modifying the 1.7 code to fit the 2 code and everything has been working fine until I get to displaying any images on the page.

I have my directory tree as
-application
-img <---- where the images are stored.
-css
-system

The gallery_model code is as follows:
Code:
class Gallery_model extends CI_Model{
var $gallery_path;
var $gallery_path_url;

function __construct(){
  parent::__construct();
  $this->gallery_path = realpath(APPPATH . "../img");
  $this->gallery_path_url = base_url().'img';
}


function do_upload(){

  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png',
   'upload_path' => $this->gallery_path
   );
  $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_ratio' => 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;
}

}

and the gallery view code as follows:
Code:
<div id="gallery">
  &lt;?php if(isset($images) && count($images)):
   foreach($images as $image): ?&gt;
   <div class="thumb">
    <a href="&lt;?php echo $image['url'];?&gt;">
     <img src="&lt;?php echo $image['thumb_url']; ?&gt;"/>
    </a>
   </div>


  &lt;?php endforeach; else: ?&gt;
  <div id="blank_gallery">Please Upload an Image</div>
  &lt;?php endif; ?&gt;
</div>
I am using the .htaccess trick to get rid of the index.php, the correct site URL is loaded in config and index.php is removed from config as well.

The pictures correctly upload, (main image in the img folder and the thumbs go into the "thumbs" folder, but if I try to view them on a page, or even call them directly (localhost/dev/img/pic.jpg) it will say 404. The links ARE showing up correctly in the View Source. I have tried Base_url, site_url, and even created a custom "asset_url()" and nothing works.

Any help would be appreciated. I have googled this problem for the past few hours and I cannot find anything similar to this problem, or someone just posted "fixed it" and didnt reply how.

EDIT: Just in case you need to see the functions called, here is the controller. Pretty simple.
Code:
class Gallery extends CI_Controller {

public function index()
{
  $this->load->model('gallery_model');
  if($this->input->post('upload')) {
   // Received an upload
   $this->gallery_model->do_upload();
  }

  $data['images'] = $this->gallery_model->get_images();
  $data['main_content'] = 'gallery';
  $this->load->view('includes/template', $data);
}

}
#2

[eluser]Aken[/eluser]
Well the first step should obviously be checking if you can view the images by typing in the URL directly in your browser. If you can't, then there's your problem right there. If you can't, it's probably an .htaccess issue.

I've always used either base_url() or an absolute path starting with root (IE: "/assets/img/pic.jpg") depending on the environment and project. Make sure your base_url config item is a full URL with trailing slash. "http://localhost/" for example.
#3

[eluser]nlweb[/eluser]
[quote author="Aken" date="1338161227"]Well the first step should obviously be checking if you can view the images by typing in the URL directly in your browser. If you can't, then there's your problem right there. If you can't, it's probably an .htaccess issue.

I've always used either base_url() or an absolute path starting with root (IE: "/assets/img/pic.jpg") depending on the environment and project. Make sure your base_url config item is a full URL with trailing slash. "http://localhost/" for example.[/quote]

I tried to access it by URL and it will not work either.
.htaccess is as follows:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt|css|js|favicon\.png)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

That .htaccess is in the root directory:
dev/
--application
--img
--css
--system
.htaccess

the IMG folder does not have an htaccess file.
#4

[eluser]nlweb[/eluser]
Thank you for pointing me in the right direction Aken. It was an htaccess file issue, I had the images folder named "img" and the rewrite rule was for an "images" folder. Such a simple thing I feel foolish. Heres to anyone who stumbles upon this later on. Thanks Aken.
#5

[eluser]Aken[/eluser]
The .htaccess is routing requests to CodeIgniter, because your "img" folder is not in the excluded list. Change the RewriteCond line to:

Code:
RewriteCond $1 !^(index\.php|img|css|robots\.txt|favicon\.png)

And adjust it as necessary when you add more assets / static directories that are not part of CodeIgniter.
#6

[eluser]InsiteFX[/eluser]
This is wrong!
Code:
// APPPATH points to the application folder.
$this->gallery_path = realpath(APPPATH . "../img");

// You want to use is FCPATH which is the path to index.php the root!
$this->gallery_path = realpath(FCPATH . "img");




Theme © iAndrew 2016 - Forum software by © MyBB