Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-11-2016, 02:54 PM   #1
Registered: Jun 2011
Location: North Easton,MA
Distribution: currently Ubuntu Linux on my netbook while job searching, but also use Cygwin GNU tools on MSXP nb
Posts: 33

Rep: Reputation: Disabled
Smile running php script from popen in Suse Linux

My question is about using popen() which forks and execs a program and returns the file pointer to the output stream from the command just run so it can be read. In this case, it's executing a PHP script.

interpret being called with path = /home/karen/dev/cs410Projects2and3_2015to2016/webserverToPhysComp/public/test/index.php

interpret: calling popen() on command:
QUERY_STRING="" REDIRECT_STATUS=200 SCRIPT_FILENAME="/home/karen/dev/cs410Projects2and3_2015to2016/webserverToPhysComp/public/test/index.php" php-cgi

I have some code that I was given where I had to write some of the code, but not this part, and I do not understand why the command sent to the popen() Linux function which forks and execs the program (this time being the PHP interpreter to run the PHP script) is formatted in this way. Can anyone explain?

From my understanding, from the linux command line, you execute a php script this way:
php -f filename.php

I thought popen() was similar. Is there a reason to format the command sent to popen in the above way? This is the code that they execute before calling popen(). The path to php file sent from the browser is in the path line above, but what is given to popen() is the command.

    // open pipe to PHP interpreter
    char* format = "QUERY_STRING=\"%s\" REDIRECT_STATUS=200 SCRIPT_FILENAME=\"%s\" php-cgi";
    char command[strlen(format) + (strlen(path) - 2) + (strlen(query) - 2) + 1];
    if (sprintf(command, format, query, path) < 0)
    printf("interpret: calling popen() on command = %s", command);
    FILE* file = popen(command, "r");
    if (file == NULL)

    // load interpreter's content
    char* content = NULL;
    size_t length;
    printf("\ninterpret: calling load() file popened = %d\n",fileno(file));
    if (load(file, &content, &length) == false)

So my question is: why can't you just send the string as command: "php -f /home/karen/dev/cs410Projects2and3_2015to2016/webserverToPhysComp/public/test/index.php"

Why does the code given add a format string and the command is then:
QUERY_STRING="" REDIRECT_STATUS=200 SCRIPT_FILENAME="/home/karen/dev/cs410Projects2and3_2015to2016/webserverToPhysComp/public/test/index.php" php-cgi

What popen() returns a valid file pointer, but once it is read, it says zero bytes read from the open stream returned from popen().

I was going to try it the way I thought it should work, as mentioned above, but I do not know PHP, and I thought perhaps there is something that the PHP interpreter being invoked may understand from the command script being sent, but then again, when the file pointer returned from popen is read, it reads zero bytes, so there is something wrong with it.

Thank you for any advice you can provide!
Old 05-11-2016, 03:56 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
php -f filename.php invocation is for cli php
For php-cgi you need to set variables before calling the php interpreter

Which this line does:
QUERY_STRING="" REDIRECT_STATUS=200 SCRIPT_FILENAME="/home/karen/dev/cs410Projects2and3_2015to2016/webserverToPhysComp/public/test/index.php" php-cgi


Depending on how php-cgi was compiled, you might need to set additional variables
see for example:

Last edited by keefaz; 05-11-2016 at 04:06 PM.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
read/write with popen in php luftwaffe Programming 4 12-28-2014 03:24 PM
Running Bash script in PHP script SteveMack2015 Linux - Software 2 05-14-2010 03:34 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
Running PHP Scripts in SuSe Linux 9.3 w/o a server. elliotfuller Linux - Software 3 06-18-2005 06:27 PM
Running a php script locally on SuSE 9.1 Personal ClarkNG Linux - Newbie 5 08-20-2004 02:55 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration