Is it possible to change image folder in cakephp 2.4 - cakephp

I have read many docs, but failed to solve the problem:
Images in my existing project stored in $_SERVER['DOCUMENT_ROOT'].'/media/'
I want, but can't change in cakephp 2.4 default image folder from $_SERVER['DOCUMENT_ROOT'].'/app/webroot/img/' to any I desire.
lib/Cake/bootstrap.php contains constants:
/**
* Path to the public images directory.
*/
if (!defined('IMAGES')) {
define('IMAGES', WWW_ROOT . 'img' . DS);
}
/**
* Web path to the public images directory.
*/
if (!defined('IMAGES_URL')) {
define('IMAGES_URL', 'img/');
}
and
Configure::write('App.imageBaseUrl', IMAGES_URL);
I tried to change:
IMAGES to $_SERVER['DOCUMENT_ROOT'].'/media/'
IMAGES_URL to /
And it doesn't work at all. All image files still points to $_SERVER['DOCUMENT_ROOT'].'/app/webroot/img/' I see it when try to render image in view:
echo $this->Html->image('/img/ride_scheme/chema.jpg', array( 'width' => 300 ));
If I created in app/webroot/img/ride_scheme image file chema.jpg it rendered, but I changed the folder in IMAGES constant in lib/Cake/bootstrap.php !!!
In documentation I see:
Constants IMAGES_URL, JS_URL, CSS_URL have been deprecated and
replaced with config vari- ables App.imageBaseUrl, App.jsBaseUrl,
App.cssBaseUrl respectively.
Constants IMAGES, JS, CSS have been deprecated.
I gues that is the problem.
Please, show me how to change image folder

According to your description, I reckon IMAGES_URL should be media/ and not just /.
Alternatively, you could add a RewriteRule to your .htaccess (in the project root) to rewrite everything to the media folder (assuming you have an Apache web server with the Rewrite module enabled):
RewriteRule ^app/webroot/img/(.*) /media/$1
Or you could create a symbolic link that links the img/ folder to your media folder (assuming you're on Linux hosting and you have ssh/shell access to your server):
cd app/webroot
rm -rf img/
ln -s ../../media img

Related

build full url for directory inside webroot in cakephp 3

I'm working in CakePHP 3.4.
I have an image outside img directory, in files directory under webroot
I have a files directory inside webroot along with css, img, js.
I tried using it like
$this->Html->image(WWW_ROOT . 'files' . DS . 'myimage.jpg')
which is creating path as
/var/www/html/myproject/webroot/files/myimage.jpg
But this is not showing image. Copying and pasting path in another tab is loading image perfectly. Also, moving image file to img directory and using $this->Html->image('myimage.jpg') is working fine.
Why it is not working ? Also, It is easy to build url for directories css, img and js like
// Outputs http://example.com/img/icon.png
$this->Url->image('icon.png', true);
OR
// Outputs /img/icon.png
$this->Url->image('icon.png');
which will result as
http://example.com/img/icon.png
I want to build url for files directory like
http://example.com/files/myfile.jpg
OR
// /files/myfile.jpg
How to build url for directories other than img, css and js.?
I'd doubt that using that path as a URL will work, as it's a filesystem path, and that's where the problem is. HtmlHelper::image() only supports URLs, relative as well as abolute ones (I guess the docs on this could be improved).
By default relative paths are expected to be relative to the /img/ folder. In your case you could pass a (web)root-relative path, like:
/files/myimage.jpg
If you want to generate an absolute URL, use the fullBase option:
$this->Html->image('/files/myimage.jpg', ['fullBase' => true])
See also
Cookbook > Views > Helpers > Html > Linking to Images

Zend framework uploading file and setting destination problems

I've got a problem with uploading a file with ZF.
This is my application structure:
//application
//images
index.php
//library
//styles
//public
//img
The application path in index.php seems to be ok:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
When i try to upload a file i get a message (such a dir does not exist).
This is my controllers code snippet:
$upload->setDestination(APPLICATION_PATH . '../public/img/');
Can anyone help please?
Your APPLICATION_PATH is not followed by a slash. Invoke setDestination() like this:
$upload->setDestination(APPLICATION_PATH . '/../public/img/');

Laravel returning root view instead of static content

I have one route:
Route::controller('/', 'HomeController');
And HomeController has one action getIndex which works correctly. However, when browsing to http://localhost:4040/public/style.css, the application returns the view from HomeController::getIndex. What gives?? Shouldn't it return the static file style.css?? I have confirmed that the CSS file does in-fact exist in the public folder.
To be clear, I'm running the app like this:
php -S localhost:4040 server.php
EDIT:
Hmmm, so the following seems to work:
1) Serve the project with:
php artisan serve
2) Reference files without the public/ prefix.
What does the php artisan serve command do so differently??
With using Route::controller(), if you look at the php artisan routes, the getIndex route has a optional parameters {one?}/{two?}/... and since that parameters have no matching pattern style.css get caught by the {one?}. This is the completely expected behavior, with index.php.
However, using php artisan serve, the server.php file gets called first.
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = urldecode($uri);
$paths = require __DIR__.'/bootstrap/paths.php';
$requested = $paths['public'].$uri;
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' and file_exists($requested))
{
return false;
}
require_once $paths['public'].'/index.php';
You can clearly see that it checks if the file is real in if ($uri !== '/' and file_exists($requested)), and if it is, it stops calling the index.php that may process it as a route parameter, and instead returns a real file.

play 2.0 serve static file

I want localhost/assets/abc.png file to be served from project_root/assets/abc.png
GET /assets/*file controllers.Assets.at(path="/public", file)
With the routes above, localhost/assets/abc.png won't be served( I couldn't find a way)
but localhost/assets/images/abc.png will be served from root/assets/images/abc.png.
Add the following entry at the bottom of routes:
# Serves only abc.png from root public folder
GET /$file<abc.png> controllers.Assets.at(path="/public", file)
That said, if you have multiple files to serve like this, better put them in a subfolder

AssetCompress for CakePHP 1.3

I'm using AssetCompress v0.5 from the 1.3 branch and the dynamic built is not working. I've done the following so far:
Downloaded and copied the asset_compress folder into my app/plugins folder
Copied the config.ini and renamed it to app/config/asset_compress.ini
Created 2 CSS and JS bundles named public and logged (so 4 in total)
Added AssetCompress.AssetCompress in my $helpers array in my AppController
Changed my default.ctp layout file to: echo $this->AssetCompress->css('public'); echo $this->AssetCompress->css('logged'); (same for ->script())
The output error is:
Error: The requested address '/asset_compress/assets/get/public.css' was not found on this server.
What am I missing?
Ok, I figured it out.
It was because I was using the CssMin filter and I did not download it and put it into the vendor folder. As the plugin seems to be turning the debug level to 0, I did not see the error, until I put a Configure::write('debug', 2); into the beforeFilter().

Resources