Serve a Directory with PHP

By  on  

Many developers have a giggle at PHP, even looking down at the language, but let's be honest:  most of our blogs are powered by it (WordPress) and it's a great language to dabble around with.  I cut my teeth on PHP, though I prefer to avoid PHP these days.

But when I need to experiment with a simple PHP coding task for the sake of a blog feature, I look for the simplest possible serving solution to accomplish that feat.  Luckily PHP provides me a quick solution for testing:

php -S localhost:8888

The command above allows for serving of a directory via PHP, thus allowing for a simple PHP "site" to be served enough to confirm that my code testing works properly.

Serving a directory with PHP, python, or Node.js ... the same problem solved for different languages!

Recent Features

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    MooTools 1.3 Browser Object

    MooTools 1.3 was just released and one of the big additions is the Browser object.  The Browser object is very helpful in that not only do you get information about browser type and browser versions, you can gain information about the user's OS, browser plugins, and...

  • By
    Highlight Table Rows, Columns, and Cells Using MooTools 1.2.3

    Row highlighting and individual cell highlighting in tables is pretty simple in every browser that supports :hover on all elements (basically everything except IE6). Column highlighting is a bit more difficult. Luckily MooTools 1.2.3 makes the process easy. The XHTML A normal table. The cells...

Discussion

  1. Joris Ros

    You can add easily a public directory to it by adding -t parameter.

    php -S localhost:8080 -t public_html
    
  2. Love tips like this, thanks for putting them together!

  3. If you’re using Linux. sudo right is required

  4. Elena

    To change a current directory function chdir() is used. It returns true on success and false on failure. Please note function chdir() works in PHP 4, PHP 5, PHP 7. Following is a description of this function.

    https://www.mindstick.com/Articles/12164/directory-handling-in-php

    http://pixelcode.co.uk/tutorials/php/directory-handling-in-php/

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!