Welcome Guest, Not a member yet? Register   Sign In
Need help getting application base path


I'm working on my 2nd CI application, and my goal is to make my applications "ultraportable" - basically upload and run.

Right now, I'm working on an image upload/delete administration section.

When a user deletes an image, a function called deleteImage in my model will get called:

function deleteImage($image_id) {
        $this->db->where('image_id', $image_id);
        $query = $this->getImageById($image_id);        //retrieve some info so we can delete the file.
        $imageinfo = $query->result();
        $imagetodelete = $imageinfo[0]->image_filename;
        $imagepath = $this->config->item('image_uploads');

        $filedelete = $imagepath . $imagetodelete;

        echo "Delete: $filedelete<br><br>";


        $this->db->where('image_id', $image_id);
        return $this->db->affected_rows();

My problem is in this:
$filedelete = $imagepath . $imagetodelete;

I need to get the FULL path to that image starting from root. I don't see a function in the CI documentation that returns the applications base directory (eg: c:\wamp\www on my dev box, or /home/user/www on prod box.)

For portability reasons, I do not want to hard code it. I'm sure I'm just missing something.


[eluser]louis w[/eluser]
CI has a var built in, $base_url

Thanks, but I am looking for the local file system path, not the URL to the application.

It looks like there's been a debate in the past over the exclusion of this.

I found these two related threads, exactly what I needed:


[eluser]louis w[/eluser]
By local file system path, do you mean the path to the current file? Try $_SERVER["SCRIPT_FILENAME"]

[eluser]Yunus Khan[/eluser]
You can create easily documentation path / base path easily..

*create base path
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . '/requiredfoldername';

Hello everybody

I just came over the same problem, an i realized, that it wasn't really one for me.

Correct me if I'm wrong, but for me it works just consider the base_path is just the working directory of the running CI script, beacause the index.php file is stored in that directory.

So why not consider
$base_path = "."

This would have the conclusion, that a subdirectory of the base_path could be accessed by something like that:

$images_dir = "images/"; //means http://www.urdomain.com/any_directory/images/

Are there any cases where the working directory of a CI application would change during system execution?


Theme © iAndrew 2016 - Forum software by © MyBB