views:

189

answers:

3

Say I have a file in my kohana 3 website called assets/somefile.jpg. I can get the url to that file by doing

 echo Url::site('assets/somefile.jpg'); // /kohana/assets/somefile.jpg

Is there a way I can get the absolute path to that file? Like if I want to fopen it or get the size of the file or something like that.

In other words, I would like to get something like /var/www/kohana/assets/somefile.jpg or W:\www\kohana\assets\somefile.jpg or whatever is the absolute path.

A: 

Try echo realpath('assets/somefile.jpg').

pinaki
That will work only if his PHP script happens to be in the project's root directory.
Pekka
Yeah, I need to use it in a class and I don't really have a clue where it will be launched from.
Svish
+1  A: 

I haven't worked with Kohana but isn't that the job of Kohana::find_file?

Finds the path of a file by directory, filename, and extension. If no extension is given, the default EXT extension will be used.

When searching the "config" or "i18n" directory, an array of files will be returned. These files will return arrays which must be merged together.

Pekka
Does that exist in Kohana 3 though? Can't find it in the api there at least: http://v3.kohanaphp.com/guide/api
Svish
@Svish it should be there: http://v3.kohanaphp.com/guide/api/Kohana (search for find_file)
Pekka
@Pekka: Aaah. Tried to search for it in the overview there. Didn't think about going in to the Kohana part there. Will try that the next time I am looking for something! Thanks! Will try out that function when I get to my workstation :)
Svish
Find file is for assets in the cascading filesystem (classes, views, etc). The file you are trying to find doesn't really apply to this.
zombor
@zombor: So in other words files that are under the `application` folder? If so, is there an alternative? Or is it the `DOCROOT` constant that someone else here mentioned I have to use?
Svish
Well, things that are in the application, modules, or system folder. The point of find_file() is to merge the filesystem and return the proper file. If you are just looking for an asset in your website like an image, this is usually not what find_file() is used for.When I said use DOCROOT, I was assuming that your file URI was something like http://localhost/assets/somefile.jpg and not a file in your kohana filesystem.
zombor
@zombor: Aha, but your assuming was correct :)
Svish
+1  A: 

I think you want:

DOCROOT.'assets/somefile.jpg'
zombor
Is that defined in the bootstrap or something? Or? Can't remember to have seen that constant anywhere...
Svish
(Will check it out first thing when I get to my computer though)
Svish
It's in your index.php. You can also use things like APPPATH, SYSTPATH and MODPATH.
zombor
Ah, ok. Will def check that out then! :D
Svish
Sorry, it should say SYSPATH above, not SYSTPATH.
zombor
@zombor: Tested it out now, and DOCROOT seems to have done the trick. Since my code might run on windows sometimes, I use this little snippet: `$absolute = DOCROOT.str_replace('/', DIRECTORY_SEPARATOR, $url)`, where `$url` might for example be `assets/somefile.jpg`. Seems to work :) Thanks!
Svish