0

I need to run a .bat script from PHP. The .bat script needs to contain file names that are PHP variables. How would I go about doing this inside PHP?

Thanks!

3
  • So, PHP calling a .bat, which invokes PHP files? Commented Sep 15, 2011 at 21:13
  • The .bat file runs the LAME audio encoder. PHP contains variables (the audio file filenames) that need to be input into the .bat file. Commented Sep 15, 2011 at 21:25
  • Then my edit should work for you. Commented Sep 15, 2011 at 21:27

4 Answers 4

1

Use fopen(), fwrite() and fclose() to create the .bat file, then use shell_exec() to execute it. See here:

fopen: https://www.php.net/manual/en/function.fopen.php

fwrite: https://www.php.net/manual/en/function.fwrite.php

fclose: https://www.php.net/manual/en/function.fclose.php

shell_exec: http://php.net/manual/en/function.shell-exec.php

=================================================================================

EDIT: Or, if it's always the same number of php variables being passed and they are scalar, you could just create one .bat file that accepts command-line parms, then pass the scalar variables in your shell_exec.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool - that was going to be plan B. Is this gonna store a batch file on my server directory for each file upload?
0

Pass variable from one programming language to another? There isn't, until specified language supports argument, if there is some (and there is), use it (pass variables by argument)

Comments

0

If your bat file is just taking name of a file and some basic parameters, then you just shell_exec() with the proper parameters filled in.

Batch file command-line params are referenced %1 %2 etc. if your batch file is foo.bat and it contains:

notepad.exe %1

you can call foo.bat textfile.txt from the command line.

To run it from php, you construct your command line as a string, then call shell_exec using it

Comments

0

To expand on Jonathan's answer, you do not have to execute the batch file with PHP. You may also use PHP only to generate a configuration batch file. You can then call this in your batch file to access all the variables you need. This is very flexible, and keeping the variables in a separate batch file keeps your main code clean.

To illustrate, you would need three files:

  1. batch.bat Your main batch file
  2. setup.php The configuration generator
  3. config.bat The configuration batch file (created by setup.php)

The configuration generator

First off, we will create the setup.php script which generates the config.bat file. It could look like this:

// Create a list of all variable names we want
// to use in the bat file, with their values.
// Note that these can come from anywhere - from
// a database, other files, remote API calls, etc.
$batVariables = array(
    'InfoFile' => 'info.txt',
    'TrackListing' => 'tracklist.txt',
    'AuthorBio' => 'author.txt',
    'MainTrack' => 'audio.mp3'
);

// create the code for the configuration
// batch file.
$batCode = 
'@echo off'.PHP_EOL.
':: AUTOMATICALLY GENERATED FILE, DO NOT EDIT'.PHP_EOL.
PHP_EOL;

// add all variables
foreach($batVariables as $varName => $value) 
{
    $batCode .= 'set '.$varName.'='.$value.PHP_EOL;
}

// save the configuration file
file_put_contents('config.bat', $batCode);

This will generate a batch file that looks like this:

@echo off
:: AUTOMATICALLY GENERATED FILE, DO NOT EDIT

set InfoFile=info.txt
set TrackListing=tracklist.txt
set AuthorBio=author.txt
set MainTrack=audio.mp3

Load the configuration

Next, add these two lines in your batch file (you will have to make sure that the php executable is accessible in your PATH system variable):

:: Generate the configuration batch file
php setup.php

:: Load the configuration with all variables
call config.bat

This will generate a fresh configuration each time the main batch file is used.

By loading the config.bat with the call command, the variables will be available directly in the scope of the main batch file. This means you can start using them as usual, like this for example:

echo Using the information file %InfoFile%.

Beyond this, you may wish to add a few error checks in case the configuration file cannot be generated. One simple way would be to ensure the file exists:

IF EXIST "config.bat" (
    call "config.bat"
) ELSE (
    echo The configuration file does not exist.
    echo.
    pause
    exit
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.