Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:23:31+00:00 2026-06-07T09:23:31+00:00

I have a script that is designed to be run both as a web

  • 0

I have a script that is designed to be run both as a web page, and via the console.

Detecting which method was used to invoke the script seems pretty straight forward, but when the script is being run from the console, I need to know if the script is being run interactively or not (user typing commands, or input redirected from a file).

php script.php
versus
php script.php < input_file

Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:23:34+00:00Added an answer on June 7, 2026 at 9:23 am

    I also needed a slightly more flexible solution than posix_isatty that could detect:

    • Is the script being run from the terminal
    • Is the script receiving data via a pipe or from a file
    • Is the output being redirected to a file

    After a bit of experimenting and digging around through libc headers, I came up with a very simple class that can do all of the above and more.

    class IOMode
    {
        public $stdin;
        public $stdout;
        public $stderr;
    
        private function getMode(&$dev, $fp)
        {
            $stat = fstat($fp);
            $mode = $stat['mode'] & 0170000; // S_IFMT
    
            $dev = new StdClass;
    
            $dev->isFifo = $mode == 0010000; // S_IFIFO
            $dev->isChr  = $mode == 0020000; // S_IFCHR
            $dev->isDir  = $mode == 0040000; // S_IFDIR
            $dev->isBlk  = $mode == 0060000; // S_IFBLK
            $dev->isReg  = $mode == 0100000; // S_IFREG
            $dev->isLnk  = $mode == 0120000; // S_IFLNK
            $dev->isSock = $mode == 0140000; // S_IFSOCK
        }
    
        public function __construct()
        {
            $this->getMode($this->stdin,  STDIN);
            $this->getMode($this->stdout, STDOUT);
            $this->getMode($this->stderr, STDERR);
        }
    }
    
    $io = new IOMode;
    

    Some example usage, to show what it can detect.

    Input:

    $ php io.php
    // Character device as input
    // $io->stdin->isChr  == true
    
    $ echo | php io.php
    // Input piped from another command
    // $io->stdin->isFifo == true
    
    $ php io.php < infile
    // Input from a regular file (name taken verbatim from C headers)
    // $io->stdin->isReg  == true
    
    $ mkdir test
    $ php io.php < test
    // Directory used as input
    // $io->stdin->isDir  == true
    

    Output:

    $ php io.php
    // $io->stdout->isChr  == true
    
    $ php io.php | cat
    // $io->stdout->isFifo == true
    
    $ php io.php > outfile
    // $io->stdout->isReg  == true
    

    Error:

    $ php io.php
    // $io->stderr->isChr  == true
    
    $ php io.php 2>&1 | cat
    // stderr redirected to stdout AND piped to another command
    // $io->stderr->isFifo == true
    
    $ php io.php 2>error
    // $io->stderr->isReg  == true
    

    I’ve not included examples for links, sockets, or block devices, but there’s no reason they shouldn’t work, as the device mode masks for them are in the class.

    (Not tested on Windows – mileage may vary)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script that run to fix my menu bar to the browser
I have a script that submits data via an ajax POST. It is called
I am writing a PHP script designed to run an executable file (ffmpeg.exe) via
I have a script that is designed to parse XML postbacks from Ultracart, right
I have written a Python script that currently is designed to handle traditional CGI
I have this Greasemonkey script that works fine in the Fiddle. It's designed to
I have an SVN repository. I have a shell/bash script that's designed to automatically
I have designed a web page where on the left there is a div
I have script that reads remote file content and writes it to local server.
I have a script that recursively loops through all the sub directories and compresses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.