PHP Get directory of current files

I am wondering if it is possible to get the full path to the file that he repeated, and then maybe split it

So: echo $ currentDir // print EX: / home / user / public_html / scrips / code / test / ha

then divide it into 3 directories, so the output will be: / home / user / public_html / scripts

Is it possible?

+3
source share
2 answers

You can get the directory where the file is currently running using __DIR__or dirname(__FILE__)in <PHP 5.3.

You can go the way and go to several levels using string manipulations.

$path = implode(
    DIRECTORY_SEPARATOR, 
    array_slice(explode(DIRECTORY_SEPARATOR, $path),0,-3)
);

Alternatively this should work ...

$path = realpath(
    rtrim($path, DIRECTORY_SEPARATOR) . str_repeat('..' . DIRECTORY_SEPARATOR, 3)
); 

realpath() ( ).

+5

:

  • ;
  • , ;
  • ;
  • , ;

:

class Image {
   private $temp_path;
   protected $upload_dir = 'imagens';

   public function directory() {
       return $this->upload_dir . DIRECTORY_SEPARATOR . $this->filename;
   }
}

// If you want only the extension:

private function _ext() {
    return strrchr($this->filename, ".");
}

, , , , , . , , - PhP, , .

If you need more information about everything, just ask.

Good luck to you.

0
source

All Articles