Jump to content

System function


Xu Wei Jie

Recommended Posts

I would not understand what does php $var1 $var2 do.....???????

 

What are you talking about, $var1 $var2?  Read the manual link I provided it will explain it a lot better than I can, it also provides many examples.

Link to comment
Share on other sites

$new_id = store_data(......);

 

The second variable basically gets its value from the statement above which I am not sure what it does.

 

That really doesn't help at all cause then we would need to know what store_data returns...

Link to comment
Share on other sites

I have found out more.

 

store_data() actually returns into $new_id another file name where objects are stored and serialized into.

 

$frame is the name of another php file.

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

will run 2 CLI processes to direct the content of $frame into an output path and the other to deserialize the objects stored in $new_id.

 

However there is something that I still don't quite understand.

Does running php cli on 2 filenames interpret both of them and direct them into the output path?

 

 

Link to comment
Share on other sites

What I heard was system function is very OS restrictive means it is not portable across platforms. Obviously I do know $frame is a php script. But i am exploring into php file i/o libraries to do the exact same thing as the command I typed does.

 

The command does take in objects from $newid into $frame which is ran by system and direct its output to a specified file

Link to comment
Share on other sites

Let's take away the $new_id which is supposed to be a file containing objects

Let src/$output_dir/$output be a directory, we will name it src/out.php

 

I will run

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

 

Let's create a dummy php file and its filename to be assigned to $frame

$frame = dummy.php

 

dummy.php

<?php

echo 'This is a dummy''

?>

 

If I ran a system command like this, it will run dummy.php and output 'This is a dummy' to src/out.php

 

If I use file i/o libraries, i can never run or achieve something like

php $frame $new_id

which is a command but I do not wish to use system command, thus I was thinking how should I work around it.

 

Placing file functions inside the script may help but it will also means a lot more lines of codes. If i can do something like what system() can do and using php in built functions only, it would be great.

 

Any ideas or alternatives?

Link to comment
Share on other sites

I really don't see the issue here.

 

Put simply, you have a php file...

 

<?php

  $new_id = 10;
  $output_file = 'src/foo/out.log';
  include 'dummy.php';

?>

 

Then, in dummy.php...

<?php

  ob_start();

  echo "The id is $new_id";
  // more script & output goes here.

  $out = ob_get_contents();
  file_put_contents($output_file, $out);
  ob_end_clean();

?>

 

You could even wrap most of that into the calling script so dummy.php would be cleaner. This would make your calling script look more like....

 

<?php

  ob_start();

  $new_id = 10;
  $output_file = 'src/foo/out.log';
  
  include 'dummy.php';

  $out = ob_get_contents();
  file_put_contents($output_file, $out);
  ob_end_clean();

?>

 

While dummy.php would simply contain....

<?php

  echo "The id is $new_id";

  // more stuff to output

?>

 

I think your making this a hell of alot more difficult than it need be, either that or I am completely missing your point.

Link to comment
Share on other sites

ob_start(); and ob_get_contents(); are new to me

The solution you gave seems viable if $new_id is just a variable.

 

Are there any file functions that given a php filename, I can run it and get the contents? I am just using pseudo code here

 

i.e. $contents = runphp('filename', second parameter....,third parameter...)

fwrite($contents to another file)

 

 

Link to comment
Share on other sites

Are there any file functions that given a php filename, I can run it and get the contents? I am just using pseudo code here

 

No, but you could easily make one.

 

<?php

function execphp($file, $args=array()) {
  ob_start();
  extract($args);
  include $file;
  $out = ob_get_contents();
  ob_end_clean();
  return $out;
}

$contents = execphp('foo.php',array('bar' => 1, 'bob' => 50));

?>

 

This would make the variables $bar and $bob exist (and contain the values 1 & 50) within foo.php, execute the code within foo.php and return any output into $contents.

Link to comment
Share on other sites

Thanks . It is quite a neat solution. However I have a problem here.

I am not interacting with integer parameters but a file name($new_id) of a file that contain objects

 

Pseudocode to simulate the command " php 'filename' $new_id "

$contents = runphp('filename', $new_id);

 

How do I code that to your execphp function?

 

Link to comment
Share on other sites

I did this. Not sure whether if I did it correctly

 

function execphp($file, $args) {

  fetch_data($args);

// 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;

}

 

I am still having some problem with it and debugging in the process

 

Link to comment
Share on other sites

I have found a problem.

The include statement does process the file but the contents are not written back to the file.

 

system command seems to run in a special way.

 

i.e.

 

if I put these lines individually in a script. they give you the same results

 

<?php include("SecurityCheck.php"); ?>

 

or

 

<?php system("php SecurityCheck.php",$retval); ?>

 

but in the background, they don't get processed the same way.

 

Thus, I was looking for an alternative to the system command. thorpe, thanks for your script but it doesn't work.

Link to comment
Share on other sites

Ignore my previous updates. I think I have found the problem

 

I am working with a file that has a special extension

 

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

 

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

 

Thus, I am still working on an alternative. If you have ideas, let me know ^.^

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.