Search code examples
imagecakephpcakephp-2.1

Unable to load image files in cakephp when call an action


At First, I load home page and all images load and it is OK. but when clicked on link and called another action (view action) it's images don't load.

it is my Images path and view.ctp file :

Images Path in webroot

And it is view action :

public function view($id = NULL) 
{  
    $this->layout= false;  
    $post = $this->Post->read(NULL, $id);  
    if(isset($this->params['requested'])){  
        return $post;  
    }  
    $this->set('post',$post);  
}  

Solution

  • You have a lack of understanding of the relative and absolute paths used in URLs or in general file systems. Let me start somewhere else:

    1. Do not place images in you CSS folder. The css folder which is already present in lower case in the default cakephp baked app is used to store CSS files. Nothing more and nothing less. Images belong in the "img" folder in the webroot directory.
    2. You should always use either complete lower case for urls and the directories and files in the webroot either it is cake or not. This is because most of the good webhosting operating systems like Debian are based on linux which makes a difference in upper and lowercase writing. The one and only OS which does not do this is Windows (as far as i know). I know you are coding on Windows this is why all you messed up code works now, but if you put it up somewhere it is usually a Linux based server you are dealing with and you can rewrite half of your appliation views.
    3. Ross solution is the right with the exception that it conflicts with the first point of this list where it says to put images in the "img" directory. You should always use CakePHPs build-in solution for paths. In this way you make sure that the app does not collapse at any point even if you may put it in a subdirectory or you host it on some stupid system like those from some german hosters...no names here.

    To you problem: There are some things you need to know about paths. There are absolute paths. Those are providing information from the root of your filesystem to the point you want to be: e.g.: /var/www/somefolder/someapp/webroot/index.php (Linux) C:\www\somefolder\someapp\webroot\index.php (Windows)

    To make this easier to handle while you are deeep in your filesystem or in an enviroment where you maybe do not even know your absolute path relative paths were "invented". Let's say you are in the directory specified above and you want to access or include the "Config/core.php" of your application: ../Config/core.php (Linux) ..\Config\core.php (Windows - nothing special here except for the slashes to be backslashes)

    What this basicly does is: going back to the parent directory of "webroot" which is in this case "/var/www/somefolder/someapp/" (Linux) and for windows "C:\www\somefolder\someapp\". From there we are going forward again to the "Config" folder and accessing the core.php afterwards.

    This schema applies to urls as well. E.g. www.example.com/pages/imprint/ . Normally the server would search for a folder "pages" in the webroot of your hosting. In there it would search for a folder "imprint". Afterwards, based on your server config, it would search in that folder for an "index.html" or "index.php" or some other file your adviced the server to use as an index file. Or it could just output the content of the folder.

    With the .htaccess file in your "[application]/webroot/" folder this behavior gets redirected to the index.php file of CakePHP. Cake is analyzing the url and checks if there are any valid controller actions or routes for it and afterwards run the appropriate code for it. But! this rule applies not on actual files or folders in the webroot directory. (Take a look at the htaccess file in the webroot folder and google for the commands used in there). This is why css files can be delivered without any problems if you are using the right syntax. Since CakePHP uses the folder url syntax for its urls giving relative urls is a problem because they are only right for one specific page most of the time. E.g. for /pages/imprint ../img/blabla.gif would go back to webroot and show the specific picture, because the url to use would be "webroot/img/blabla.gif". But in the same application /products/view/1 the path ../img/blabla.gif would end up in "webroot/products/img/blabla.gif" which is not an actual file, cakephp would try to process it and it would fail because the url is not valid.

    I hope this helps out.

    Checkout those paths a little more. Understanding those is essential for programming and it helped me a lot back then ;)

    Greetings func0der