Php root directory localhost

Run a PHP Web Server to Quickly Serve Local Files and Folders From Any Directory

The most basic requirement while working on websites and web apps is the ability to see our results in a web browser. We do this by serving the local source code via a HTTP web server and then visiting http://localhost in our browser to see the results. Traditionally we’d install and configure web servers like Apache or Nginx but if you’re working on a PHP app or a static website and already have PHP installed in your system, you wouldn’t need to go the traditional route.

You can use the built-in PHP web server to quickly serve PHP and other static files (HTML, CSS, images, videos, fonts, etc.) from your current working directory or any other path in your local file system.

Built-in Web Server

The PHP CLI language interpreter ships with a built-in web server that can be fired up like this:

# Serve index.php or index.html from the current working directory $ php -S localhost:8080 # Serve a specific file $ php -S localhost:8080 specific-file.php

The syntax of the command looks like this:

$ php [options] -S addr:port [-t docroot]

You can always go through the different options by reading up on man php but as far as serving local files and folders are concerned all we need to know is that pass your desired host ( addr ) and port to -S and if you want, you can specify the document root with the -t option. If no document root is specified, the current working directory is served.

$ pwd /home/user # Serve cwd as docroot $ php -S 0.0.0.0:8080 PHP 7.2.24-0ubuntu0.18.04.10 Development Server started at Sat Feb 5 09:54:51 2022 Listening on http://0.0.0.0:8080 Document root is /home/rish # docroot Press Ctrl-C to quit. # Serve cwd/pub/ as docroot $ php -S 0.0.0.0:8080 -t pub/ PHP 7.2.24-0ubuntu0.18.04.10 Development Server started at Sat Feb 5 09:55:51 2022 Listening on Document root is /home/user/pub # docroot Press Ctrl-C to quit

Whenever the document root ( http://localhost ) or a folder ( http://localhost/foo ) is accessed via an HTTP request, the web server will look for an index.php or index.html file in the respective file system folder. If none of them are found, the parent directories will be looked up for those files until the document root is reached. Eventually an HTTP 404 Not Found will be thrown if the files aren’t found.

Читайте также:  Серверы php mysql apache

Router Scripts

While passing a specific file to the web server, it is super important to note that the file will be treated as a “router” script in such cases. An example:

$ php -S localhost:8080 page.php

Now regardless of whatever URL that you visit – /foo , /foo/bar.php , /baz.php , /style.css , etc. – page.php will always be executed.

  • If page.php returns false for a particular HTTP request, then the requested resource (file) will be processed and served.
  • If page.php does not return false for a request then any output produced by itself be returned to the client (browser).

This makes the in-built PHP web server similar to the rewrite modules of other servers like Apache and Nginx. This makes the web server compatible with any PHP web framework (Laravel, Symfony, etc.) that take advantage of URL re-writes to route all the requests through a single index.php and eventually invoke different routes and controllers internally.

File (Extension) Support

The PHP web server can serve the following static file extensions and will send the standard MIME types (content types) while serving them – .3gp, .apk, .avi, .bmp, .css, .csv, .doc, .docx, .flac, .gif, .gz, .gzip, .htm, .html, .ics, .jpe, .jpeg, .jpg, .js, .kml, .kmz, .m4a, .mov, .mp3, .mp4, .mpeg, .mpg, .odp, .ods, .odt, .oga, .ogg, .ogv, .pdf, .pdf, .png, .pps, .pptx, .qt, .svg, .swf, .tar, .text, .tif, .txt, .wav, .webm, .wmv, .xls, .xlsx, .xml, .xsl, .xsd, and .zip .

Any other extension like .rb (Ruby), .py (Python), etc. will be served off as Content-Type: application/octet-stream which will lead to a file download in the web browser.

Limitations

The PHP web server is easy and quick to setup so that one can get started with serving static and PHP files or even apps written on popular PHP frameworks. There are a few limitations though that we must be aware of:

  • The server runs a single-threaded process. This means new requests will “stall” if a previous request is blocked due to performing time consuming operations like network or disk IO, data computation, etc.
  • Due to the previous reason, never use it in production environments. For production it is always recommended to use a more sophisticated and scalable web server like Apache, Nginx, etc.

Источник

Find Root Directory Path in PHP

Find Root Directory Path in PHP

  1. Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHP
  2. Use the dirname() Function to Find the Path of the Root Directory of a Project in PHP
  3. Use $_SERVER[‘DOCUMENT_ROOT’] to Find the Document Root Directory of a File in PHP

We will introduce different methods to find the path of the root directory of a PHP project.

Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHP

In PHP, there are predefined constants that can be used to achieve various functionalities. __DIR__ is one magical constant that returns the complete file path of the current file from the root directory. It means it will return the file’s directory. dirname(__FILE__) can also be used for the same purpose.

Suppose we have a project folder which is the root directory of the project. The project folder has the following file path /var/www/HTML/project . Inside the project folder, we have the index.php file and another folder master . Inside the master folder, we have two PHP files: login.php and register.php .

project ├── index.php └── master  ├── login.php  └── register.php 

Suppose we are currently working on login.php . In such a file structure, we can get the directory’s path using the __DIR__ constant in the login.php file. We can use the echo function to print the constant.

Use the dirname() Function to Find the Path of the Root Directory of a Project in PHP

The function dirname(__FILE__) is similar to __DIR__ . We can find the path of the directory of a file using this function. We can also move to the upper levels in the file path using the dirname() function. The first parameter of the function is the path of the file, which is denoted by the __FILE__ constant. The second parameter is an integer which is called levels. We can set the levels to direct the function to level up in the file path. The default value of the level is 1 . As we increase the level, the function will get the file path of one level up. So, we can use this function to find the exact file path of the project’s root directory in PHP.

For example, we can consider the file structure as the first method. Working from the file, login.php , we can use the dirname() function with level 2 and the __FILE__ constant as parameters. Then we can get the exact file path of the working directory. Thus, we can change the levels according to our choice to move upward and downward in the file path. In this way, we can find the path of the root directory of the project in PHP.

php echo dirname(__FILE__,2); ?> 

Use $_SERVER[‘DOCUMENT_ROOT’] to Find the Document Root Directory of a File in PHP

We can use the $_SERVER[] array with the DOCUMENT_ROOT indices to find the document root directory of the currently executing script. It will return the complete path of the document root directory. It is defined in the configuration file in the server. For the file structure above, we can print the $_SERVER[‘DOCUMENT_ROOT’] with the echo function to find the document root directory of the file login.php .

As shown in the output below, we found out the path html is the document root directory of the login.php file. We can see the file path of the root directory as well.

php echo $_SERVER['DOCUMENT_ROOT']; ?> 

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Related Article — PHP File

Источник

Php root directory localhost

This web server is designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network.

The CLI SAPI provides a built-in web server.

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, the lookup for index.php and index.html will be continued in the parent directory and so on until one is found or the document root has been reached. If an index.php or index.html is found, it is returned and $_SERVER[‘PATH_INFO’] is set to the trailing part of the URI. Otherwise a 404 response code is returned.

If a PHP file is given on the command line when the web server is started it is treated as a «router» script. The script is run at the start of each HTTP request. If this script returns false , then the requested resource is returned as-is. Otherwise the script’s output is returned to the browser.

Standard MIME types are returned for files with extensions: .3gp, .apk, .avi, .bmp, .css, .csv, .doc, .docx, .flac, .gif, .gz, .gzip, .htm, .html, .ics, .jpe, .jpeg, .jpg, .js, .kml, .kmz, .m4a, .mov, .mp3, .mp4, .mpeg, .mpg, .odp, .ods, .odt, .oga, .ogg, .ogv, .pdf, .pdf, .png, .pps, .pptx, .qt, .svg, .swf, .tar, .text, .tif, .txt, .wav, .webm, .wmv, .xls, .xlsx, .xml, .xsl, .xsd, and .zip.

Changelog: Supported MIME Types (file extensions)

Version Description
5.5.12 .xml, .xsl, and .xsd
5.5.7 .3gp, .apk, .avi, .bmp, .csv, .doc, .docx, .flac, .gz, .gzip, .ics, .kml, .kmz, .m4a, .mp3, .mp4, .mpg, .mpeg, .mov, .odp, .ods, .odt, .oga, .pdf, .pptx, .pps, .qt, .swf, .tar, .text, .tif, .wav, .wmv, .xls, .xlsx, and .zip
5.5.5 .pdf
5.4.11 .ogg, .ogv, and .webm
5.4.4 .htm and .svg
Changelog
Version Description
7.4.0 You can configure the built-in webserver to fork multiple workers in order to test code that requires multiple concurrent requests to the built-in webserver. Set the PHP_CLI_SERVER_WORKERS environment variable to the number of desired workers before starting the server. This is not supported on Windows.

This experimental feature is not intended for production usage. Generally, the built-in Web Server is not intended for production usage.

Example #1 Starting the web server

$ cd ~/public_html $ php -S localhost:8000

Источник

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