Up one level php

Get 2 levels up from dirname( __FILE__)

With PHP7 go further up the directory tree by specifying the 2nd argument to dirname . Versions prior to 7 will require further nesting of dirname .

Is the main benefit from this method over dirname(__FILE__).’/../’; to remove the possible inconsistency of DIRECTORY_SEPARATOR?

@Patrick I would say that the main benefit of this over your suggestion is that we get the absolute path of the directory instead of a relative path. Also, DIRECTORY_SEPARATOR inconsistencies are generally edge cases as PHP automatically converts *nix style separators into the appropriate Windows style separator for most cases.

what if you had to go 3+ levels up? Would be nice if dirname’s 2nd parameter was $levels , thus dirname(__FILE__, 3); 🙂

@Patrick another strategy to ensure the correct DIRECTORY_SEPARATOR is uses is to wrap the path in realpath()

Even simpler than dirname(dirname(__FILE__)); is using __DIR__

Читайте также:  Style color white css

which works from php 5.3 on.

[ web root ] / config.php [ admin ] [ profile ] / somefile.php 

How can you include config.php in somefile.php? You need to use dirname with 3 directories structure from the current somefile.php file.

require_once dirname(dirname(dirname(__FILE__))) . '/config.php'; dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file 

As suggested by @geo, here’s an enhanced dirname function that accepts a 2nd param with the depth of a dirname search:

/** * Applies dirname() multiple times. * @author Jorge Orpinel * * @param string $path file/directory path to beggin at * @param number $depth number of times to apply dirname(), 2 by default * * @todo validate params */ function dirname2( $path, $depth = 2 )

Note: that @todo may be relevant.

The only problem is that if this function is in an external include (e.g. util.php) you can’t use it to include such file :B

Источник

PHP How to Go One Level Up on Dirname(_File_)

. but in a web server environment you will probably find that you are already working from current file’s working directory, so you can probably just use:

. to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

You should also be aware what __DIR__ and __FILE__ refers to:

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

Get folder up one level

. but in a web server environment you will probably find that you are already working from current file’s working directory, so you can probably just use:

. to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

You should also be aware what __DIR__ and __FILE__ refers to:

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

Get 2 levels up from dirname( __FILE__)

PHP 5.2 and lower

return dirname(dirname(__FILE__));

With PHP7 go further up the directory tree by specifying the 2nd argument to dirname . Versions prior to 7 will require further nesting of dirname .

file_get_contents with relative path

$json = file_get_contents(__DIR__ . '/../validate/edit.json');

__DIR__ is a useful magic constant.

For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.

When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.

PHP will often report that it is unable to find the third file, but why?
Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.

In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.

So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.

php how to go two level up on dirname(__FILE__)

Источник

PHP Script to Redirect Up-One-Level

This script reliably redirects to the directory one level above the current location in the web server’s path.

For example, if you use the script as an index file at

www.example.com/site/files/index.php 

it will instantly redirect the browser to

I searched for a rock-solid way to redirect the browser up-one-level and didn’t find anything quite like I expected to find. Then I searched some more, wrote this PHP script, and created this page to publish and document it.

License : You are free to do whatever you want with the script.

The Script

Alternate Version

This version is essentially the same. Minor differences:

Installation

Usually you would use it as a directory index file.

Configuration

No configuration is necessary. The script is reliable and compatible.

  • It auto-adapts to PHP variables that can change from one server to the next.
  • It auto-adapts to HTTP or HTTPS protocol.
  • It redirects to a full URL path without «..» (a relative path), so it’s compatible with any browser.

Example Commands

$ wget http://haganfox.net/site/uploads/Main.PhpRedirectUpOneLevel/index-php.txt $ mv index-php.txt index.php 
$ wget http://haganfox.net/site/uploads/Main.PhpRedirectUpOneLevel/index2-php.txt $ mv index2-php.txt index.php 

Answers

Most often you will use the script in directories where you want to deny a directory listing. Someone doing «URL hacking» is quickly and silently redirected up one level. The redirection is virtually instant.

What if my server is configured (e.g. using .htaccess to deny directory listings?

In that case, the script still works as an index file. Your visitor is redirected rather than shown a Forbidden or Listing Denied message (HTTP 403 status code).

What directories does it belong in?

Any web-accessible directory without an index file, probably. It’s up to you.

Why append the trailing slash?

Directory paths end with a slash, so a slash is appended. The trailing slash avoids an unnecessary extra redirect. (Apache.org reference)

Why is there no closing tag?

A ?> closing tag is omitted intentionally. (PHP.net reference)

Источник

php — Get folder up one level

Solution:

. but in a web server environment you will probably find that you are already working from current file’s working directory, so you can probably just use:

. to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

Answer

Solution:

echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'); 

Answer

Solution:

echo dirname(__DIR__); 

But note the __DIR__ constant was added in PHP 5.3.0.

Answer

Solution:

Also you can use dirname(__DIR__, $level) for access any folding level without traversing

Answer

Solution:

The parent directory of an included file would be

e.g. the file is /var/www/html/folder/inc/file.inc.php which is included in /var/www/html/folder/index.php

then by calling /file/index.php

getcwd() is /var/www/html/folder __DIR__ is /var/www/html/folder/inc so dirname(__DIR__) is /var/www/html/folder 

but what we want is /var/www/html which is dirname(getcwd())

Answer

Solution:

To Whom, deailing with share hosting environment and still chance to have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible to use following.

function dirname_safe($path, $level = 0) < $dir = explode(DIRECTORY_SEPARATOR, $path); $level = $level * -1; if($level == 0) $level = count($dir); array_splice($dir, $level); return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; >print_r(dirname_safe(__DIR__, 2)); 

Answer

Solution:

For example if you want to access a folder above the root directory from anywhere

$_SERVER['DOCUMENT_ROOT']."/../myfolder/spl-auto.php" 

or for your example C:\UwAmp\www\myfolder\admin

$admin = $_SERVER['DOCUMENT_ROOT']."/myfolder/admin/" $myfolder = $_SERVER['DOCUMENT_ROOT']."/myfolder/" 

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

HTML

HTML (English «hyper text markup language» — hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Оцените статью