JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Run Shell Scripts and Open Shell Files in PHP

Author:JIYIK Last Updated:2025/04/12 Views:

PHP allows us to use shell_exec();the shell function to process files. However, if your operating system is Windows, you should consider using the popen()and pclose()functions, because pipes are executed in text mode, which usually prevents their binary output.

We will implement two scripts in our shell.php./openfile.txt file. First, we will use the shell_exec();./openfile .shfunction.

We will then use shell_exec()to open a cmd interface and run some windows commands.


shell_exec()Run the shell file in text mode using

Function Syntax and Parameters: shell_exec(string $cmd);. This function returns the shell output in string format.

We created a demo demo.shfile in this tutorial.

Code ( demo.sh):

#!/bin/sh
echo "Hello world";
echo "This is a shell .sh file for demo";
// your shell commands go here

You can create a .txt file using any text editor .shand .shsave it with the .txt file extension. After that, run the following PHP script ( shell.php) to open it in Notepad as it will throw in the string formatting in text mode.

<!DOCTYPE html>
<head>
  <title>
    Run Shell File in PHP and open CLI (shell) to run shell scripts 
     </title>
</head>
</head>
   <form action="shell.php" method ="post" align="center">
      <input type="submit" value="Open .sh file using shell_exec() in PHP" name="openshellfile" />
      <input type="submit" value="Run cmd/shell on Windows using shell_exec() in PHP" name="opencmd" />

   </form>
    </body>
        </html>
<?php
// When user submits the form 
if(isset($_POST['openshellfile'])){
echo $res=shell_exec('PATH to the file/demo.sh');
}
?>

Output:

Open shell file with php


Use in CLI shell_exec()to return binary format

shell_exec()Functions can be used for a variety of things. Our method is shell_exec()a great way to use without having to run a function in the background.

You also don't need to use popen()or pclose().

<?php
// Ping facebook cmd (shell)
// open cmd shell
if (isset($_POST['opencmd']))
{
    //You do not need to use popen() or pclose() either to run this shell command
    // you can add any command here, it will work!
    //For example, you can control your Windows (CLI)
    //You will only change the command after cmd.ex /k "Your Command"
    $open = shell_exec('start cmd.exe /k ping "facebook.com"');
    echo $open;
    //function shell($open) {
    //check your php version
}
?>

While we could have run any command with our script, we only used ping facebook.com. For example, if you wanted to open a calculator through this function, enter calcinstead of ping "facebook.com".

Before adding the above code in your PHP script, you first need to change the name of the input field in the HTML. Then add the above code to shell.phpthe file we ran earlier.

The scope of the command can be anything, you can type any windows command and assign it to $openthe variable. The script will run your command directly to the Windows shell (CLI in this example).

Output:

Run cmd command with php

shell_exec();We executed a cmd command to ping in the above code using facebook.com. However, you can type any command in the function argument and it will work.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial