Multi-purpose Server Scripting with PHP and Bash

Share

WWW is a great “advanced platform” in the current society. Linux is commonly used in web servers. The terminal in Linux is also well known to be powerful. In Linux most of the things could be done with the terminal. Bash is the Unix shell and a powerful commanding language which is in both Linux and Mac OS. With bash, a shell script could be created for any commanding in Linux and Mac OS.

There’re always web applications to do most of the things which were used to be done with  just a computer such as converting an audio into another format, video into audio etc. So how to code something like that or even do things more advanced? You may be thinking of creating a web application to your working place which manipulates text files and excel files, to insert data into a file in a server etc. Here we’re going to give some hints for doing that. Those may not be the best solutions, but still they may help you think out of the box, from the things you’re learning about coding.

Think it’s possible to join bash and php together in a Linux platform. If it’s possible, you may be able to build powerful web applications.

If you’re going to join bash and php together, first thing you need is a way to run and pass parameters to bash script. In that case there’s a php command as given below to execute a shell command.

shell_exec("command");

It returns the resulting output of the command you have placed.

"<yoastmark

Now what you have to try is to execute a bash script with that command to test whether it works or not. First of all you need a good bashing knowledge to code a useful bash script. You could try the TLDP (The Linux Documentation Project) to learn the bash.

Let’s have a small bash script saved as script.sh in the /srv/ directory which echos Advanced Server Scripting.

Bash Script which Echos a String
Bash Script which Echos a String

So let’s try the terminal command which executes the bash script with the php.

Executing bash script with php
Executing bash script with php

Still there’s a security problem in having the script in the /srv/ directory. To resolve this and to use the script system wide as a command, you could rename it and move it to the /usr/bin/  directory. Here I have changed the name of the script to serverEg. Most importantly, you should give permission to the script to execute as a program.

Moving script to /usr/bin/ and giving required permission
Moving script to /usr/bin/ and giving required permission

Since you have the script placed in the /usr/bin/ directory, you could directly execute it like follows.

Executing bash script directly
Executing bash script directly

As an example, you could use the following code to create a web application which converts mp3 audio to wave audio format.

Bash Script Code

#!/bin/bash

input=$1
extension=$2

target="/srv/http/convert/converted$extension"

if [[ -e $target ]]; then
     rm $target
fi
sox $input $target

output=${target#/srv/http}

if [[ -e $target ]]; then
     echo "<a href=\"$output\">Download the Converted File</a>"
else
     echo "<p style=\"color:red;\">Error in converting.</p>"
fi

 

PHP Code

<?php
  $target_dir = "/srv/http/uploads/";
  $target_file = $target_dir . "uploaded.mp3";
  $uploadOk = true;

  if(isset($_POST["submit"])) {
    if($_FILES["upload"]["type"] == "audio/mp3") {
      $uploadOk = true;
    } else {
      $uploadOk = false;
    }

    if($uploadOk){
      move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file);
    }
  }
?>

<!DOCTYPE html>
<html>
  <body>
    <br />
    <h2>MP3 to Wav Converter</h2>
    <br /><br /><br /><br />
    <form action="server.php" method="post" enctype="multipart/form-data">
      Select mp3 file to upload (200MB Max):
      <input type="file" name="upload" id="upload" />
      <input type="submit" value="Upload MP3" name="submit" />
    </form>
    <br /><br /><br />

<?php
  if(isset($_POST["submit"])) {
    if(!($uploadOk)){
      echo "<p style=\"color:red;\">Error: Please upload a mp3 file.</p>";
    }else{
      $msg = shell_exec("convert $target_file .wav");
      echo $msg;
    }
  }
?>

  </body>
</html>
Audio Converter Front-end
Audio Converter Front-end

Important:

You need to install sox package (Sound eXchange) before running the script and give required permission to both uploads and convert directories.

 

If you have any ideas to improve this article or any question about this, please comment.

 
Tagged : / / /