Xu Wei Jie Posted March 4, 2009 Share Posted March 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/ Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 it should work properly if u have given correct path to include. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-776600 Share on other sites More sharing options...
Xu Wei Jie Posted March 4, 2009 Author Share Posted March 4, 2009 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 ...... Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-776609 Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 use include_once () Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-776639 Share on other sites More sharing options...
Xu Wei Jie Posted March 4, 2009 Author Share Posted March 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-776707 Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 you will have to create and destroy objects as u r including the file and only one instance is running it multiple times. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-776814 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 Managing one instance is hard as the scripts include the scripts in a tree like manner Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777031 Share on other sites More sharing options...
trq Posted March 5, 2009 Share Posted March 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777063 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777066 Share on other sites More sharing options...
trq Posted March 5, 2009 Share Posted March 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777077 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777082 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777111 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777159 Share on other sites More sharing options...
trq Posted March 5, 2009 Share Posted March 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777213 Share on other sites More sharing options...
Xu Wei Jie Posted March 5, 2009 Author Share Posted March 5, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777240 Share on other sites More sharing options...
trq Posted March 5, 2009 Share Posted March 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-777244 Share on other sites More sharing options...
Xu Wei Jie Posted March 6, 2009 Author Share Posted March 6, 2009 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................ Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-778033 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 I seriously think you need to rethink your entire design. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-778533 Share on other sites More sharing options...
samshel Posted March 6, 2009 Share Posted March 6, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-778619 Share on other sites More sharing options...
Xu Wei Jie Posted March 7, 2009 Author Share Posted March 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-779181 Share on other sites More sharing options...
trq Posted March 8, 2009 Share Posted March 8, 2009 If your application is console based then you defintaley don't have a problem using system to fork new php processes. Im really not sure what your big issue with system is. Quote Link to comment https://forums.phpfreaks.com/topic/147966-executing-a-script-using-file-functions/#findComment-779327 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.