PHPackages                             rosell-dk/exec-with-fallback - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. rosell-dk/exec-with-fallback

ActiveLibrary[CLI &amp; Console](/categories/cli)

rosell-dk/exec-with-fallback
============================

An exec() with fallback to emulations (proc\_open, etc)

1.2.0(4y ago)47.3M—5.2%1[2 issues](https://github.com/rosell-dk/exec-with-fallback/issues)4MITPHPPHP ^5.6 | ^7.0 | ^8.0

Since Nov 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/rosell-dk/exec-with-fallback)[ Packagist](https://packagist.org/packages/rosell-dk/exec-with-fallback)[ GitHub Sponsors](https://github.com/rosell-dk)[ Fund](https://ko-fi.com/rosell)[ RSS](/packages/rosell-dk-exec-with-fallback/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (4)

Exec with fallback
==================

[](#exec-with-fallback)

[![Latest Stable Version](https://camo.githubusercontent.com/59a336555f62cef45be1c1621308d28102df6b0475cc2d2d7ade3252f633a57a/687474703a2f2f706f7365722e707567782e6f72672f726f73656c6c2d646b2f657865632d776974682d66616c6c6261636b2f76)](https://packagist.org/packages/rosell-dk/exec-with-fallback)[![Minimum PHP Version](https://camo.githubusercontent.com/1287eafcc1040ef69347811e2a43df87a46bf12b57c80e2d9cf908922b8020f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f726f73656c6c2d646b2f657865632d776974682d66616c6c6261636b2f7068702e737667)](https://packagist.org/packages/rosell-dk/exec-with-fallback)[![Build Status](https://camo.githubusercontent.com/aa24c8910ed728fc27d0b82aeb6d9556327e5409d4534cda20e540f84448965c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f73656c6c2d646b2f657865632d776974682d66616c6c6261636b2f63692e796d6c3f6c6f676f3d476974487562267374796c653d666c61742d737175617265)](https://github.com/rosell-dk/exec-with-fallback/actions/workflows/ci.yml)[![Coverage](https://camo.githubusercontent.com/39524cff4e44931025e9a058086cdf9111f95b57a88abf3fb86276956556bac4/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f6c6974746c652d622e69742f657865632d776974682d66616c6c6261636b2f636f64652d636f7665726167652f636f7665726167652d62616467652e6a736f6e)](http://little-b.it/exec-with-fallback/code-coverage/coverage/index.html)[![Software License](https://camo.githubusercontent.com/f2232f3af24374ee6c8aa2865a8815d571c0f169ce6a2da161c061504ab61a8b/687474703a2f2f706f7365722e707567782e6f72672f726f73656c6c2d646b2f657865632d776974682d66616c6c6261636b2f6c6963656e7365)](https://github.com/rosell-dk/exec-with-fallback/blob/master/LICENSE)[![Daily Downloads](https://camo.githubusercontent.com/c8bf160ec18ec73ae0232066d62cb40c01b603f25afc9c80404786b4ae291d95/687474703a2f2f706f7365722e707567782e6f72672f726f73656c6c2d646b2f657865632d776974682d66616c6c6261636b2f642f6461696c79)](https://packagist.org/packages/rosell-dk/exec-with-fallback)

Some shared hosts may have disabled *exec()*, but leaved *proc\_open()*, *passthru()*, *popen()* or *shell\_exec()* open. In case you want to easily fall back to emulating *exec()* with one of these, you have come to the right library.

This library can be useful if you a writing code that is meant to run on a broad spectrum of systems, as it makes your exec() call succeed on more of these systems.

Usage:
------

[](#usage)

Simply swap out your current *exec()* calls with *ExecWithFallback::exec()*. The signatures are exactly the same.

```
use ExecWithFallback\ExecWithFallback;
$result = ExecWithFallback::exec('echo "hi"', $output, $result_code);
// $output (array) now holds the output
// $result_code (int) now holds the result code
// $return (string | false) is now false in case of failure or the last line of the output
```

Note that while the signatures are the same, errors are not exactly the same. There is a reason for that. On some systems, a real `exec()` call results in a FATAL error when the function has been disabled. That is: An error, that cannot be catched. You probably don't want to halt execution on some systems, but not on other. But if you do, use `ExecWithFallback::execNoMercy` instead of `ExecWithFallback::exec`. In case no emulations are available, it calls *exec()*, ensuring exact same error handling as normal *exec()*.

If you have `function_exists('exec')` in your code, you probably want to change them to `ExecWithFallback::anyAvailable()`

Installing
----------

[](#installing)

`composer require rosell-dk/exec-with-fallback`

Implementation
--------------

[](#implementation)

*ExecWithFallback::exec()* first checks if *exec()* is available and calls it, if it is. In case *exec* is unavailable (deactivated on server), or exec() returns false, it moves on to checking if *passthru()* is available and so on. The order is as follows:

- exec()
- passthru()
- popen()
- proc\_open()
- shell\_exec()

In case all functions are unavailable, a normal exception is thrown (class: Exception). This is more gentle behavior than real exec(), which on some systems throws FATAL error when the function is disabled. If you want exactly same errors, use `ExecWithFallback::execNoMercy` instead, which instead of throwing an exception calls *exec*, which will result in a throw (to support older PHP, you need to catch both Exception and Throwable. And note that you cannot catch on all systems, because some throws FATAL)

In case none succeeded, but at least one failed by returning false, false is returned. Again to mimic *exec()* behavior.

PS: As *shell\_exec()* does not support *$result\_code*, it will only be used when $result\_code isn't supplied. *system()* is not implemented, as it cannot return the last line of output and there is no way to detect if your code relies on that.

If you for some reason want to run a specific exec() emulation, you can use the corresponding class directly, ie *ProcOpen::exec()*.

Is it worth it?
---------------

[](#is-it-worth-it)

Well, often these functions are often all enabled or all disabled. So on the majority of systems, it will not make a difference. But on the other hand: This library is easily installed, very lightweight and very well tested.

**easily installed**
Install with composer (`composer require rosell-dk/exec-with-fallback`) and substitute your *exec()* calls.

**lightweight**
The library is extremely lightweight. In case *exec()* is available, it is called immediately and only the main file is auto loaded. In case all are unavailable, it only costs a little loop, amounting to five *function\_exists()* calls, and again, only the main file is auto loaded. In case *exec()* is unavailable, but one of the others are available, only that implementation is autoloaded, besides the main file.

**well tested**
I made sure that the function behaves exactly like *exec()*, and wrote a lot of test cases. It is tested on ubuntu, windows, mac (all in several versions). It is tested in PHP 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2. It is tested in different combinations of disabled functions (ie "exec" disabled, "passthru" disabled, both "exec" and "passthru" disabled, etc). The giant test of these combinations is run before each release. In additional to this, nightly builds with future PHP versions are tested once a month, allowing me to be avare of potential problems early. Status: [![Giant test](https://camo.githubusercontent.com/b440250fc5ccda3027de7e33bddd4aee492e205ac5bcc73fd907186c62a49838/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f73656c6c2d646b2f68746163636573732d6361706162696c6974792d7465737465722f72656c656173652e796d6c3f6c6f676f3d476974487562267374796c653d666c61742d737175617265266c6162656c3d4769616e7425323074657374)](https://github.com/rosell-dk/htaccess-capability-tester/actions/workflows/release.yml) [![PHP 8.3 nightly](https://camo.githubusercontent.com/f4abe6b344a5d7055b26959fb13675892cfb9e2a6c62551be8dffa187d227e35/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f73656c6c2d646b2f68746163636573732d6361706162696c6974792d7465737465722f70687038332e796d6c3f6c6f676f3d476974487562267374796c653d666c61742d737175617265266c6162656c3d504850253230382e332532306e696768746c79)](https://github.com/rosell-dk/htaccess-capability-tester/actions/workflows/php83.yml)[![PHP 8.4 nightly](https://camo.githubusercontent.com/6d32ac8e8af4b22996957bf9dbab47389ff419181f65af5819fd7153757f8ae4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f73656c6c2d646b2f68746163636573732d6361706162696c6974792d7465737465722f70687038342e796d6c3f6c6f676f3d476974487562267374796c653d666c61742d737175617265266c6162656c3d504850253230382e342532306e696768746c79)](https://github.com/rosell-dk/htaccess-capability-tester/actions/workflows/php84.yml)

**going to be maintained**
The library is usid in [webp-convert](https://github.com/rosell-dk/webp-convert) and [webp-express](https://wordpress.org/plugins/webp-express/), both of which are used in many projects. So it is widely used and any future problems are bound to be discovered. While I don't expect much need for maintenance for this project, it is going to be there, if needed.

**Con: risk of being recognized as malware**There is a slight risk that a lazy malware creator uses this library for his malware. The risk is however very small, as the library isn't suitable for malware. First off, the library doesn't try *system()*, as that function does not return output and thus cannot be used to emulate *exec()*. A malware creator would desire to try all possible ways to get his malware executed. Secondly, malware creators probably don't use composer for their malware and would probably want a single function instead of having it spread over multiple files. Third, the library here use a lot of effort in getting the emulated functions to behave exactly as exec(). This concern is probably non-existant for malware creators, who probably cares more about the effect of running the malware. Lastly, a malware creator would want to write his own function instead of copying code found on the internet. Copying stuff would impose a chance that the code is used by another malware creator which increases the risk of anti malware software recognizing it as malware.

Do you like what I do?
----------------------

[](#do-you-like-what-i-do)

Perhaps you want to support my work, so I can continue doing it :)

- [Become a backer or sponsor on Patreon](https://www.patreon.com/rosell).
- [Buy me a Coffee](https://ko-fi.com/rosell)

Thanks!

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

5

Last Release

1623d ago

Major Versions

0.9 → 1.0.02021-12-01

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17592708?v=4)[Bjørn Rosell](/maintainers/rosell-dk)[@rosell-dk](https://github.com/rosell-dk)

---

Top Contributors

[![rosell-dk](https://avatars.githubusercontent.com/u/17592708?v=4)](https://github.com/rosell-dk "rosell-dk (129 commits)")

---

Tags

commandexecfallbackopen\_procsturdyresiliant

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rosell-dk-exec-with-fallback/health.svg)

```
[![Health](https://phpackages.com/badges/rosell-dk-exec-with-fallback/health.svg)](https://phpackages.com/packages/rosell-dk-exec-with-fallback)
```

###  Alternatives

[league/climate

PHP's best friend for the terminal. CLImate allows you to easily output colored text, special formats, and more.

1.9k14.0M273](/packages/league-climate)[league/tactician

A small, flexible command bus. Handy for building service layers.

86415.4M127](/packages/league-tactician)[mrrio/shellwrap

Use any command-line tool as a PHP function.

738198.8k2](/packages/mrrio-shellwrap)[nategood/commando

PHP CLI Commando Style

8123.3M38](/packages/nategood-commando)[helhum/typo3-console

A reliable and powerful command line interface for TYPO3 CMS

2939.0M192](/packages/helhum-typo3-console)[adhocore/cli

Command line interface library for PHP

3501.2M50](/packages/adhocore-cli)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
