So i've got a python script, which is being executed using the exec() command through embedded PHP. Currently, the python script takes just one file (uploaded using a standard HTML form), but I want to pass it multiple files, and have it run through them every time. I've tried the following code based on some examples I saw here on StackOverflow, but can't seem to get it to work. Does anyone know how I can do it? I just need to get the tmp_file name and the name, then pass them to the exec function, I want it to run once for each file (with the respective information for each file).
<?php
foreach ($_FILES["inputFile"]{
$dataIn = $_FILES["inputFile"]["tmp_name"];
$originalName = $_FILES["inputFile"]["name"];
exec("python /home/will/public_html/OrderAnalyser.py $dataIn $originalName 2>&1",$output);
foreach ($output as $out){
echo $out;
echo "<br />";
}
}
?>
Related
I am collecting a series of php files and testing to see if a single function returns valid output. To facilitate the process, all their functions are named identically. So then I can run:
foreach($fileList as $file) {
require($file);
echo $testFunction();
}
The problem is that php throws an error 'Cannot redeclare function' since the second file's function is named the same as the first. What I want to do is 'undeclare' a function after I test its output but I know this isn't possible and I'm trying to handle this procedurally. unlink($file) does not remove the instance of the function, unfortunately. Is there a simple way to handle this without using an OOP approach?
UPDATE #1
Using exec() instead of shell_exec() allows me to check err status (which is #2). CHMOD was necessary as user/group prevented execution (security settings on this offline server to be updated once the script is functioning). At this point, it does not echo anything since shell_exec() is returning an error (at least I think so since the output from shell_exec is empty and since exec is returning error #2). Here is an updated test:
$fileList = array('test.php');
foreach($fileList as $file) {
// load code from the current file into a $code variable,
// and append a call to the function held in the $testFunction variable
$code = file_get_contents($file) . "\n" . 'testFunction();';
// save this to a temporary file
file_put_contents('test-file.php', $code);
// execute the test file in a separate php process,
// storing the output in the $output variable for examination
//*************** */
$output=null;
$retval=null;
$absPath = realpath('test-file.php');
chmod($absPath,0777);
echo $absPath;
exec($absPath, $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
}
UPDATE #2
While you can't undeclare a function, you can repeatedly assign different functions to the same var. For example:
$listOfFunctionNames = array('function1', 'function2', 'function3);
foreach($listOfFunctionNames as $func) {
$funxion = $func;
$funxion();
}
You can execute the files in another process, for example (assuming $testFunction is defined in the files), you could do something like this (assuming you are running on Linux):
foreach($fileList as $file) {
// load code from the current file into a $code variable,
// and append a call to the function held in the $testFunction variable
$code = file_get_contents($file) . "\n" . '$testFunction();';
// save this to a temporary file
file_put_contents('/tmp/test-file.php', $code);
// execute the test file in a separate php process,
// storing the output in the $output variable for examination
$output = shell_exec('php /tmp/test-file.php');
// examine output as you wish
}
unlink('/tmp/test-file.php');
EDIT:
Since testFunction does not echo, and instead returns the output to be examined, we can simply modify the test file to echo testFunction();.
$code = file_get_contents($file) . "\n" . 'echo testFunction();'; // <- NOTE: the semi-colon after testFunction();
I noticed my original answer was lacking a semi-colon in the test file, which is probably where the error was coming from. What you can do to ensure it's correct is have this script generate the first test file and terminate early. You can then manually inspect the file for correctness and also use PHP to ensure it's parse-able, from the command line:
php -l /tmp/test-file.php
Note also there are more sophisticated ways you could check correctness of each test file, however I am trying to keep the answer concise, as that is starting to stray into a separate question.
actual I finished writing my program. Because it is only a plugin and it runs on a external server I still want to see if I get some errors or something else in the console.
I wrote every console input with echo ...;. My question now is if it is possible to get the text of the console?
Because then I could easily safe it in a .txt file and could get access to it from the web :) - Or is there another way to get the console text?
I could probably just say fwrite(...) instand of echo ...;. But this will cost a lot of time...
Greetings and Thank You!
An alternative that could be usefull on windows would be to save all the output buffer to a txt, first check your php configuration for the console app implicit_flush must be off then
<?php
ob_start(); //before any echo
/** YOUR CODE HERE **/
$output = ob_get_contents(); //this variable has all the echoes
file_put_contents('c:\whatever.txt',$output);
ob_flush(); //shows the echoes on console
?>
If your goal is to create a text file to access, then you should create a text file directly.
(do this instead of echoing to console)
$output = $consoleData . "\n";
$output .= $moreConsoleData . "\n";
(Once you've completed that, just create the file:)
$file = fopen('output.txt', 'a');
fwrite($file, $output);
fclose($file);
Of course, this is sparse - you should also check that the file exists, create it if necessary, etc.
For console (commando line interface) you can redirect the output of your script:
php yourscript.php > path-of-your-file.txt
If you haven't access to a command line interface or to edit the cronjob line, you can duplicate the starndar output at the begining of the script:
$fdout = fopen('path-to-your-script.txt', 'wb');
eio_dup2($fdout, STDOUT);
eio_event_loop();
fclose($fdout);
(eio is an pecl extension)
If you are running the script using the console (i.e. php yourscript.php), you can easily save the output my modifying your command to:
php yourscript.php > path/to/log.txt
The above command will capture all output by the script and save it to log.txt. Change the paths for your script / log as required.
I'm currently creating a php page which does a ssh call with a .rb (ruby) file.
rb file
require 'metainspector'
page = MetaInspector.new("www.hln.be")
puts page.image
When creating a php file with the following code (php):
$cmd = "ruby facescrape.rb";
$last_line = system($cmd, $retval);
echo $last_line . '
echo $retval;
this only returns value 1.
However 2 things :
When running the same command in ssh, it will print the page.image
correctly.
When i change the rb file and for instance set as last line
puts "test"
this value returns correctly with also print correctly with the aboven php code.
I don't get why printing the page.image works in ssh but won't work by using that php code.
Also tried using exec() instead of system().
Thank you in advance!
Kind regards,
Kurt Colemonts
I am writing an application which takes text input from the user to generate a text file. A bash script, which takes the generated text file as input should generate another script as output.
I tried using exec command but I am not sure if it works. I want something like this:
exec('generate.sh input.txt generated.sh');
generate.sh takes two inputs:
input.txt file - has user input from PHP page(text separated by '\n')
generated.sh - name of the generated file(to be generated by 'generate.sh').
How can I execute the bash script from PHP to get the above output?
Thanks in advance.
The whole argument to exec needs to be a string like this:
exec('generate.sh input.txt generated.sh');
$generate = '/path/to/generate.sh';
$input = '/path/to/input.txt';
$generated = '/path/to/generated.sh';
if(file_exists($generate))
{
chmod($generate, 0755);
if(file_exists($input))
{
exec($generate.' '.$input.' '.$generated);
}
else
{
echo "Input file not found: ".$input."<br />";
}
}
else
{
echo "Generate file not found: ".$generate."<br />";
}
Furthermore you can also consider system(), shell_exec(), passthru() or proc_open() to execute external commands.
Trying to figure this out. I am trying to execute a perl script within php, using shell_exec() like so:
<?php
$output=shell_exec("./tst.pl > test.txt");
//$output=shell_exec("./tst.pl");
echo $output;
?>
It will not write output to a file using ">" filename.txt.
It will work if I execute without directing it to a filename as I can confirm this with echo.
Does this have to do with using ">"?
Permissions should be fine as I am able to run the same perl script on command line and direct to file. Any suggestions for executing this?
The output of "test.txt" will be used as input:
<?php
$data = array();
$InputFile = file("test.txt");
...
?>
It was definitely a permissions problem. Wrote the file out to /tmp and it worked fine.
<?php
$output=shell_exec("./tst.pl > /tmp/test.txt");
echo $output;
?>