3

I'm using a bash script to fire a PHP script but I can't seem to get the variable into PHP...

Bash script where $1=mymusic.mp3

php /var/www/html/wave/waveform.php $1;

PHP

$mp3 = '/var/www/html/processed/' . $argv[1];
copy($mp3, "$tmpname}_0.mp3");

The $argv[1] variable is just not being received by PHP. Any ideas?

4
  • How are you running the bash script? Can you share the entire bash script? Commented Jun 8, 2016 at 1:46
  • It's ok, I installed php5-cgi, changed it accordingly and it's working fine now, just couldn't get it to work this way at all. Commented Jun 8, 2016 at 2:23
  • 2
    You must have register-argc-argv directive enabled in php.ini. Commented Jun 8, 2016 at 7:00
  • @baf This was the problem, thank you ;) Commented Jun 8, 2016 at 11:13

3 Answers 3

5

You say in a comment that you are using php5-cgi to run the script.

php5-cgi is the CGI version of the interpreter; its purpose is to be used by the web server.

If you want to run the script on the command line then you have to use the CLI version of the interpreter. It's name is php (or, possibly, php5 on your system).

The CLI and CGI versions of the interpreter handle some things differently. The content of the variables $argc and $argv is one of these differences.

The CGI version populates them only if the register_argc_argv option is set to On (or 1) in php.ini. For performance reasons, this option is usually Off for the CGI.

On the other hand, the CLI version populates $argc and $argv variables with the parameters received in the command line no matter the value of register_argc_argv option.

As @andlrc also notes in their answer, you should enclose $1 in double quotes when you construct the command line in the shell script, to prevent the shell splitting it into words:

php /var/www/html/wave/waveform.php "$1"

If, for example, the value of $1 is foo bar (two words), your original usage renders to php waveform.php foo bar and print_r($argv) displays:

Array
(
    [0] => waveform.php
    [1] => foo
    [2] => bar
)

On the other hand, if you enclose $1 in quotes, the command line becomes php waveform.php "foo bar" and the shell passes foo bar as a single argument to the PHP script. A print_r($argv) outputs:

Array
(
    [0] => waveform.php
    [1] => foo bar
)

As a side note, there is a missing { in copy($mp3, "$tmpname}_0.mp3");. It should probably read "copy($mp3, "${tmpname}_0.mp3");

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

3 Comments

Maybe it wasn't working as I was using php instead of php5? Just to clarify, what exactly is wrong with the way I'm doing it now if it works without fault? Are there security issues?
Yes the missing curly bracket was just a typo on here, not in the actual script ;)
There are differences between the CGI and CLI versions. For example, $argc and $argv are usually not set by the CGI. Also, it imposes a limit (default: 30 seconds) on the running time of the script; there is no time limit on the CLI version. The CGI also produces HTTP headers that are not needed when running the script using the command line. The CLI version doesn't produce headers (neither implicit nor when the function header() is called). For all the differences, please read the documentation: php.net/manual/en/features.commandline.differences.php
2

$argv[1] should work, consider this example:

% php -r 'var_dump($argv);' hello
array(2) {
  [0]=>
  string(1) "-"
  [1]=>
  string(5) "hello"
}

You should however quote $1 or it will undergo word splitting and globbing:

php /var/www/html/wave/waveform.php "$1"

Comments

-1

I couldn't get this working any way I tried it so in the end I just installed php5-cgi and now call it like this...

BASH

php-cgi -f /var/www/html/wave/waveform.php var=$1;

PHP

$filename = $_GET["var"];
$mp3 = '/var/www/html/processed/' . $filename;
copy($mp3, "$tmpname}_0.mp3");

Worked perfectly first time.

Hope it helps somebody else as I wasted a good couple of hours trying to get it working in various other ways without success.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.