Jump to content

Executing a script using file functions?


Xu Wei Jie

Recommended Posts

I hope the title is appropriate. This is with reference to the post (http://www.phpfreaks.com/forums/index.php/topic,240442.0.html).

 

It is quite an old post so I guess no one will be reading yet I can't delete so I will be rephrasing the problem here.

 

What I am trying to achieve:

system("php $frame $new_id", $retval); is an OS dependent function call

(http://sg2.php.net/system)

 

I wish to simulate the execution of a php script using php file functions instead of system() so it can be more portable across platforms

 

What are the things known?

$frame contains the filename of the php script

$new_id contains the filename of the file that contains objects

$retval is just a variable that contains the status of the executed command

 

The system function call passes the objects to the php script and executes

 

What has been discussed:

Thorpe suggested this wonderful function to me which I think can work but I have an issue with it

 

function execute_php($file, $filethatcontainobjects) {

  fetch_data($filethatcontainobjects);

  // this is a function to fetch the data from a file to initialize the objects

  ob_start();

  include $file;

  $out = ob_get_contents();

  ob_end_clean();

  return $out;

}

 

Note that the statement "include $file" interprets the $file and its output is being buffered

 

What is the issue?

I am working with $file that has a special extension

 

system("php example.xxx.php",$retval) can work fine as long there are php tags to be interpreted in there. I have tested.

 

However, include("example.xxx.php"); is not able to work. I think it has to do with the include function itself. Thus the above function execute_php cannot work properly.

 

Anyone can help me work around this problem? TIA

Link to comment
Share on other sites

Thanks. I figured it out. It is not the problem with the include function but the way my scripts are ran.

 

Because in every script of mine, I have include("xxx.php") at the beginning of the scripts. Calling a system function to execute a script will redeclare and reinitialize the functions written in xxx.php each time.

 

However if I use an include function to execute a script, it will call xxx.php and command line will prompt

<?php Fatal error: Cannot redeclare ......

Link to comment
Share on other sites

I have a question.

 

Will using ob_start() have any issues? if I am doing a recursive execute_php(...) which calls itself several times when scripts 'include' scripts?

 

Currently, I am facing problems with the objects. Doing a system call seems to be much more easier because all the objects are destroyed and recreated. But now I have to manually attend to the creation and destroyal of objects each time execute_php() is called.

Link to comment
Share on other sites

However, include("example.xxx.php"); is not able to work. I think it has to do with the include function itself. Thus the above function execute_php cannot work properly.

 

Why does include not work. You need to explain the issue.

Link to comment
Share on other sites

The include does work. Sorry for the misunderstanding. I clarified it in the reply above. Now I am more like having problems handling the objects passed to the script that is being executed because my scripts include scripts that branches out a tree like manner

Link to comment
Share on other sites

Now I am more like having problems handling the objects passed to the script that is being executed because my scripts include scripts that branches out a tree like manner

 

And the problems are? Were not mind readers. Describe your problems.

Link to comment
Share on other sites

How shall I describe it?

 

Basically when I do

 

system("php $frame $new_id > src/$output_dir/$output",$retval); It works fine because all objects are new in $frame and initialize with object data from the file $new_id

 

When I do

function execphp($file, $new_id) {

 

  reinitialize();//to reinitialize objects with empty values

  $id = fetch_data($new_id);//to get data from file $args

  ob_start();

 

  include $file;

  $out = ob_get_contents();

 

  ob_end_clean();

 

  return $out;

}

 

It seems the fetch_data is not handling the objects well such that there are duplicate values or the object values get corrupted. I am currently debugging. I don't think the problem lies with the execphp

Link to comment
Share on other sites

Ok here I start describing about my debugging findings

 

Initially I am doing something close to this

 

for(...)

{

  $new_id=1; // name of the temp file is 1

 

  $frame =.... // I assign different filenames to it and execute

  $output = $frame . "output";

  system("php $frame $new_id > src/$output",$retval);

}

 

The files that are executed goes like this

 

<?php include("foo.php")?>

<?php

$new_id=1;

$frame = "anotherfilename.php";

system("php $frame $new_id");

?>

 

foo.php contains objects to be used and some function declarations. System() works fine because multiple instances of foo.php are spawned. There is no object ambiguity.

 

Using execphp, some problems are met using ob_start

 

for(...)

{

  $new_id=1; // name of the temp file is 1

 

  $frame =.... // I assign different filenames to it and execute

  $output = $frame . "output";

  $contents = execphp("php $frame $new_id");

  //write contents to src/$output

}

 

The files that are executed goes like this

 

<?php include_once("foo.php")?> // as advised by samshel

<?php

$new_id=1;

$frame = "anotherfilename.php";

execphp("php $frame $new_id");

?>

 

I am dealing with one instance of foo.php here and there is a lot of object ambiguity.

 

I am implementing ob_start and ob_end_clean() for execphp that needs to direct output to another file. I am writing a separate execphp2 as below. This is because there are files which I simply want to include into the current context and not writing to another file.

 

function execphp($file, $tempfile) {

 

  ob_start();

  reinitialize();

  $id = fetch_data(1);

  include $file;

  $out = ob_get_contents();

  ob_end_clean();

 

  return $out;

}

 

function execphp2($file, $tempfile) {

  reinitialize();

  $id = fetch_data($tempfile);

  include($file);

}

 

Using System(), a file may execute another file easily with objects that are different instances of each other.

 

Using execphp, I am having difficulties like "where is my object?", "is it echoed to standard buffer?", "if so, how come it is echoed so many times","Why are the objects corrupted?"

 

This is a sample structure of the files that I am running

 

test0.php

<?php include_once("foo.php");?>

<?php

$contents = execphp("test1.php",1);

//write $contents to file

?>

 

test1.php

<?php include_once("foo.php");?>

<?php

execphp2("test2.php",1)

$contents=execphp("test3.php",1)

//write $contents to file

?>

 

test2.php

<?php include_once("foo.php");?>

This is a simple text file with no php tags

 

test3.php

<?php include_once("foo.php");?>

<?php

$contents=execphp("test4.php",1);

//write $contents to file

?>

and it goes on....

 

Thus, you can see how object manipulation and ob_start buffering can be hard here. Or am I thinking too complicated?

 

Any comments?

 

 

Link to comment
Share on other sites

This is what I did -

 

system("php $frame $new_id > src/$output_dir/$output",$retval);

 

Contents of the temporary file $new_id after the command above is executed

 

a:7:{s:3:"att";s:8:"JobTitle";s:10:"attributes";a:2:{i:0;s:8:"FullName";i:1;s:8:"JobTitle";}s:6:"module";s:7:"Project";s:14:"editable_title";s:12:"Edit Project";s:16:"editable_message";s:28:"Please edit the Project data";s:15:"editable_action";s:4:"edit";s:19:"editable_nextAction";s:11:"confirmEdit";}

T

O:8:"Inserter":1:{s:7:"inserts";a:1:{s:18:"editable_container";a:1:{s:6:"insert";a:1:{i:0;s:32:"

Here are the container links

";}}}}

T

N;

T

 

After which I ran an alternative solution

$contents = execphp($frame,$new_id);

$fp = fopen("src/$output_dir/$output", 'w');

fwrite($fp, $contents);

fclose($fp);

 

This is the temporary file $new_id after the commands above has been executed.

 

a:7:{s:10:"attributes";a:2:{i:0;s:8:"FullName";i:1;s:8:"JobTitle";}s:6:"module";s:5:"Staff";s:14:"editable_title";s:9:"New Staff";s:16:"editable_message";s:27:"Please enter the Staff data";s:15:"editable_action";s:6:"create";s:19:"editable_nextAction";s:18:"confirmNewInstance";s:3:"att";s:8:"JobTitle";}

T

O:8:"Inserter":1:{s:7:"inserts";a:1:{s:18:"editable_container";a:1:{s:6:"insert";a:6:{i:0;s:32:"

Here are the container links

";i:1;s:32:"

Here are the container links

";i:2;s:32:"

Here are the container links

";i:3;s:32:"

Here are the container links

";i:4;s:32:"

Here are the container links

";i:5;s:32:"

Here are the container links

";}}}}

T

N;

T

 

Both files differ and I suspect it has to do with the store function and now I am trying to debug.

 

function store_data($inserter=null) {

    global $values, $values_temp, $adapt_inserter, $id;

    $new_id = $id + 1;

   

    $combined_values = array_merge($values_temp, $values);

    $df = fopen("tmp/$new_id", 'w');

   

    // Write each objects with delimiter 'T'.

    fwrite($df, serialize($combined_values) . "\nT\n");

    fwrite($df, serialize($adapt_inserter) . "\nT\n");

    fwrite($df, serialize($inserter) . "\nT\n");

    fclose($df);

   

    return $new_id;

}

 

Any help would be appreciated

 

 

Link to comment
Share on other sites

Can you please use


tags when posting code?

 

Also, I seriously suspect you are overcomplicatin ghtis entire process. If you could describe exactly what it is your trying to do, there is alikely a better solution.

Link to comment
Share on other sites

Maybe there are too much words to complicate things. I try to summarize and pardon me for not using code tags. I am still new at posting.

 

I am implementing ob_start and ob_end_clean() for execphp that needs to direct output to another file. I am also writing a separate execphp2 as below. This is because there are files which I simply want to include into the current context and not writing to another file.

 

function execphp($file, $tempfile) {

  ob_start();
  $id = fetch_data(1);
  include $file;
  $out = ob_get_contents();
  ob_end_clean();

  return $out;
}

 

function execphp2($file, $tempfile) {
  ob_start(); 
  
  $id = fetch_data($tempfile);
  include($file);
  $out = ob_get_contents();
  ob_end_clean();
  
  echo $out;
}

 

 

These are how the files are ran

 

<?php
for(...)
{
  $new_id=1; // name of the temp file is 1

  $frame =.... // I assign different filenames to it and execute
  $output = $frame . "output";
  $contents = execphp("php $frame $new_id");
  //write contents to src/$output
}
?>

 

test0.php

<?php include_once("foo.php");?>
<?php
$contents = execphp("test1.php",1);
//write $contents to file
?>

 

test1.php

<?php include_once("foo.php");?>
<?php
execphp2("test2.php",1)
$contents=execphp("test3.php",1)
//write $contents to file
?>

 

test2.php

<?php include_once("foo.php");?>
This is a simple text file with no php tags

 

test3.php

<?php include_once("foo.php");?>
<?php
$contents=execphp("test4.php",1);
//write $contents to file
?>

 

Please note that foo.php defines all the objects needed to interact with the scripts.

 

Objective:

I am trying not to use system() because it makes the solution not portable across platforms. Thus, I am using execphp() which you have suggested.

 

Problem:

I can only use 1 instance of foo.php instead of multiple instances because execphp() does not really execute a file but simulates executing the file. Thus, I got lost trying to track the creation / destroyal and saving of objects.

 

What is happening in the background:

Each script interacts with foo.php objects by setting/getting its variables or arrays. They also save or fetch objects from a temporary file using foo.php defined functions. I named the temporary file '1'.

 

This is the store function

 

function store_data($inserter=null) {
    global $values, $values_temp, $adapt_inserter, $id;
    $new_id = $id + 1;
   
    $combined_values = array_merge($values_temp, $values);
    $df = fopen("tmp/$new_id", 'w');
   
    // Write each objects with delimiter 'T'.
    fwrite($df, serialize($combined_values) . "\nT\n");
    fwrite($df, serialize($adapt_inserter) . "\nT\n");
    fwrite($df, serialize($inserter) . "\nT\n");
    fclose($df);
   
    return $new_id;
}

 

This is the fetching function

 

/**
* Fetch data from temporary file. This is done during initialization phase.
*
* @param $data_file the file name of the temporary file.
*/
function fetch_data($data_file) {

    global $values, $adapt_inserter;
    $df = fopen("tmp/$data_file", 'r');
    $values = read_object($df);
    $adapt_inserter = read_object($df);
    $adapt_inserter->merge(read_object($df));
    return $id;
}

Link to comment
Share on other sites

I am trying not to use system() because it makes the solution not portable across platforms.

 

What makes you say that? Unless you are executing OS specific commands (php is not) then system is completely portable. In your case, php's cli would of course need to be installed on the system in question.

 

I can only use 1 instance of foo.php instead of multiple instances because execphp() does not really execute a file but simulates executing the file. Thus, I got lost trying to track the creation / destroyal and saving of objects.

 

Um, that phpexec function I wrote does actually execute the code within the file you pass to it.

Link to comment
Share on other sites

An update on this problem which I have just realized.

 

The solution I have requires a tree of nodes kind of data structure. Each node needs to 'include' a separate instance of foo.php. I realized that managing 1 instance of foo.php to cater for all the nodes is impossible as they will be sharing the objects in foo.php instead of each node having their very own foo.php's object instances.

 

Thus the execphp() implementation (which only can 'include_once' foo.php)  does not match up to using system() which is able to spawn many foo.php instances.

 

But I am still reluctant on using system() because it is not a portable function................

Link to comment
Share on other sites

i think u can achieve this by the following method...

 

include ("http://www.website.com/path_to_file/includedfile.php");

 

while including use URL to include, this will be as good as firing system command and creating seperating instances of the included file..without system dependency.

 

how ever this will work if you included file echoes out something...

 

i m not sure whether it will work or not..but worth a try i guess....

 

PS: for this to work, all ur included files should be in the web enabled directory...

Link to comment
Share on other sites

My application is console based. Thus a web approach would not be suitable. Anyway, thorpe, you mentioned that system() is portable? Another concern of mine is that my idea of a php application, programmers seldom will come to use system() and it will be weird. Correct me if I am wrong

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.