PHP Get name of current directory
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
You can use basename()
to get the trailing part of the path :)
In your case, I'd say you are most likely looking to use getcwd()
, dirname(__FILE__)
is more useful when you have a file that needs to include another library and is included in another library.
Eg:
main.php
libs/common.php
libs/images/editor.php
In your common.php
you need to use functions in editor.php
, so you use
common.php
:
require_once dirname(__FILE__) . '/images/editor.php';
main.php
:
require_once libs/common.php
That way when common.php is require'd
in main.php
, the call of require_once
in common.php
will correctly includes editor.php
in images/editor.php
instead of trying to look in current directory where main.php
is run.
PHP : Get current directory name
You just want the last element of an array:
$page_name = dirname(__FILE__);
$each_page_name = explode('/', $page_name);
echo end($each_page_name);
PHP - current folder / directory name
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
php get URL of current file directory
I've found a solution here:
https://stackoverflow.com/a/1240574/7295693
This is the code I'll now be useing:
function get_current_file_url($Protocol='http://') {
return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__));
}
how to get a path of my working folder in php?
define('ROOT', realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR));
you can use
__FILE__
How to reference the current directory name, filename and file contents with RecursiveDirectoryIterator loop?
//How do I reference the file here to obtain its contents?
$widget_text = file_get_contents(???);
$files_widgets
is a SplFileInfo, so you have a few options to get the contents of the file.
The easiest way is to use file_get_contents
, just like you are now. You can concatenate together the path and the filename:
$filename = $files_widgets->getPathname() . '/' . $files_widgets->getFilename();
$widget_text = file_get_contents($filename);
If you want to do something funny, you can also use openFile
to get a SplFileObject. Annoyingly, SplFileObject doesn't have a quick way to get all of the file contents, so we have to build a loop:
$fo = $files_widgets->openFile('r');
$widget_text = '';
foreach($fo as $line)
$widget_text .= $line;
unset($fo);
This is a bit more verbose, as we have to loop over the SplFileObject to get the contents line-by-line. While this is an option, it'll be easier for you just to use file_get_contents
.
Get the current directory name via php script in another directory
I just recreated all of this setup and was able to get base dir-name using:
echo basename($_SERVER["REQUEST_URI"]);
in /includes/header.php
.
Make sure to have your .htaccess code like this:
Options +Indexes
IndexOptions FancyIndexing
IndexOptions FoldersFirst IgnoreCase XHTML NameWidth=*
IndexOptions SuppressHTMLPreamble SuppressRules HTMLTable
IndexOptions IconHeight=16 IconWidth=16
IndexOptions SuppressDescription
AddType text/html .php
Addhandler application/x-httpd-php .php
HeaderName /includes/header.php
ReadmeName /includes/footer.html
PHP: Get last directory name from path
Easiest way would be to use basename($yourpath)
as you can see here: http://php.net/basename
Related Topics
Get Number of Rows Matched by Update Query with PHP MySQLi
Explode a Paragraph into Sentences in PHP
Laravel 4.1: How to Paginate Eloquent Eager Relationship
PHP File_Get_Contents Does Not Work on Localhost
PHP Array_Intersect() Efficiency
How to Download a File Using Curl in PHP
How to Login Using Github, Facebook, Gmail and Twitter in Laravel 5.1
Utf-8 Not Working in HTML Forms
Passing Data via Modal Bootstrap and Getting PHP Variable
Best Practice for Error Handling Using Pdo
Uploading a File via Ajax with PHP
Change Innerhtml of a PHP Domelement
How to Keep Whitespace Formatting Using PHP/Html