Welcome Guest, Not a member yet? Register   Sign In
image location different between model and view
#1

[eluser]sawatdee[/eluser]
I have some uploaded photos that I store in my /models/photos directory and my model returns an array of them based on some information. My model can not find them under /photos or ./photos. It can only see them under "./system/application/models/photos", which tells me that the approot is the working directory and it must be relative. If I try "/system/application/models/photos", then my model can not find them. But if I return "./system/application/models/photos/photoset/photo.jpg", my view does not display them. If I hard code "/system/application/models/photos/photoset/photo.jpg" (without the leading dot), my view can display them just fine. site_url () and BASEPATH will not work because I am not using an .htaccess file, so they both return approot/index.php. Even an absolute directory (/usr/local/apache2/....) did not work, and I really don't like my web directory showing up in my html files anyway. It took me 4 hours of trial and error to figure out why I could not see my images, and I still don't know what would be the most graceful way to handle this. Is there any way to know exactly what my model and view consider to be their root directory so that I can pass relative directories between them?

UPDATE:
So I did something like this in my model:
$viewDir = "/system/application/models/photos";
$modelDir = '.'.$viewDir;

I use $modelDir to find the files and I return $viewDir. However, I also have a custom library that I load in my controller and use in my view to get width and height info. So in my view, I have to pass '.'.$viewDir to my library.

It works, but it's clunky and awkward. There must be a better way.
#2

[eluser]sandwormusmc[/eluser]
Have you tried base_url()?

Put this code in your helpers folder under system (call it asset_helper or something):

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function linkMe($fileType,$fileName){
    $CI =& get_instance();
    $base_url  = $CI->config->item('base_url');
    $base_url .= '/resources/'.$fileType.'/'.$fileName;
    return $base_url;
}

Then you can call that from your scripts as such:

Code:
script language='javascript' src='&lt;?=linkMe('js','yourKillerJSLib.js')?&gt;'></script

So basically you'll create subdirectories under your CI/resources path, i.e. images, or css, or js, or whatever, really ...

Works pretty well for me.
#3

[eluser]sawatdee[/eluser]
[quote author="sandwormusmc" date="1201657016"]Have you tried base_url()?[/quote]

It's a good idea, and a similar function is in the system url helper. But base_url returns a URL. I need to open the files and read data out of them. So I need to access them as directory files, not URLs.
#4

[eluser]wiredesignz[/eluser]
don't put assets in your models folder.

add a custom config file to your application. add the photos directory absolute path.

EDIT: base_url() shouldn't return index.php, only site_url() should.




Theme © iAndrew 2016 - Forum software by © MyBB