executing a Windows command line program with system call in PHP - php

I am trying to execute a program using a system call inside a php file like so:
$newname = 'C:\Users\Jack\Desktop\upload\test.ppt' ;
$program = '"C:\Program Files (x86)\Softinterface, Inc\Convert PowerPoint\ConvertPPT"';
$input = ' /S "'. $newname .'"'
$destination = ' /T "C:\Users\Jack\Desktop\upload\\"';
$switch = ' /C 18';
$command = $program . $input . $destination . $switch;
system($command);
For some reason, the program enters an infinite loop (the browser never stops loading). I have tried the command manually and it works perfectly (takes about 2 sec to complete) but when executing it with a php file doesn't work.

use the backtick(`) symbol to wrap around the command you want to run.

Related

how to call php page with 3 arguments from powershell

below is my PS1 code to call PHP. But I want to know how to call the PHP page with 3 arguments and how to get it in PHP.
$workflow_id = 170
$task_num = 3
$next_script = 'testing.php'
$PhpExe = "C:\Admin\bin\php\php7.4.26\php.exe"
$PhpFile = "C:\Admin\www\xpress\"+$next_script+" "+$workflow_id+" "+$task_num
echo $PhpFile
$PhpArgs = '-f "{0}"' -f $PhpFile
$PhpOutput = & $PhpExe $PhpArgs
echo $PhpOutput // could not open file. but the file is present in this path
// testing.php
<?php include("commons/connection.php"); ?>
<?php
$workflow_id = $argv[1];
$task_num = $argv[2];
$update_pass = "UPDATE workflow_details SET Status ='mm' where `Workflow_Number` = $workflow_id and Work_type = 'PrepWork' and Task_Number = $task_num " ;
$status_result=mysqli_query($con,$update_pass);
?>
I am getting could not open file. while running ps1
Your attempt to call your PHP script from PowerShell is flawed in two respects:
$PhpArgs is a single string, which is therefore passed as a single argument, whereas you need to pass -f and the PHP script file name/path as individual arguments.
You're not passing any arguments to pass through to the PHP script.
Therefore, invoke $PhpExe as follows:
# Call script file $PhpFile with the values of $workflow_id and $task_num
# as arguments, and capture the output in variable $PhpOutput
$PhpOutput = & $PhpExe -f $PhpFile -- $workflow_id $task_num

How to run a background process by PHP in windows?

I try to run a Python script as a background process from PHP on Windows using exec() on this way:
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
exec("$py_bg $python $py_script $py_stdout $py_stderror &");
The script called and worked correctly, but PHP still waiting for the script.
I removed the end & as I foundout it's only work on Linux and after searching other Q&A find this sulotion:
exec("start /B $py_bg $python $py_script $py_stdout $py_stderror");
But same result. How can I solve this problem?
=== UPDATE:
I used start /B in the wrong way, I changed my code to this:
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
$py_cmd = "$python $py_script $py_arg_1 $py_std $py_stde";
pclose(popen("start /B ". $py_cmd, "a"));
But now a Warning in PHP for popen():
Warning: popen(start /B ...,a): No error in C:\wamp\www\lab\start.php on line 50
and an other for pclose():
Warning: pclose() expects parameter 1 to be resource, bool given in ...
I checked PHP: popen - Manual and see there a is not a valid mode, but I see this on several answers around here!
however:
The mode. Either 'r' for reading, or 'w' for writing.
By changing mode to r, the script call and run in the background correctly and there is not an error or warning on PHP or Py.
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
$py_cmd = "$python $py_script $py_arg_1 $py_std $py_stde";
pclose(popen("start /B ". $py_cmd, "r"));

bash echoing value to process, php loop (the process) not reading stdin

Background:
I'm in a position where I'm placing data into the command line and I need a php loop (what will become a server of sorts) to read STDIN and just echo what it reads to the shell its running in.
The following terrible code works when the process is running in the same shell as the content echoed:
<?php
echo getmypid();
$string = "/proc/" . getmypid() . "/fd/0";
while (true) {
fwrite(STDOUT, fgets(fopen($string, 'r'), 4096) . " worked\n");
}
?>
I've tried many variants:
<?php
echo getmypid();
$string = "/proc/" . getmypid() . "/fd/0";
while (true) {
$fo = fread(STDIN, 1024);
fwrite(STDOUT, $fo);
}
?>
The problem is that whenever I write to this loop from a separate terminal, the output appears in the other terminal but is not processed by the loop.
When I enter text in the same terminal, the text is echoed right back.
I need a way to get command line data into this loop from any source.

How can I pass a text file created by php to my python script for processing?

I have a php code that is writing the user input on the webpage into a text file. I wish to pass the text file into my python script that looks like follows:
PHP Script (getfile.php)
<?php
function writetofile($file, $content, $writeType){
$fo = fopen($file, $writeType);
if($fo){
fwrite($fo,$content);
fclose($fo);
}
}
?>
Python Script (predict.py)
clf=joblib.load('model.pkl')
def run(command):
output = subprocess.check_output(command, shell=True)
return output
row = run('cat '+'/Users/minks/Documents/X-test.txt'+" | wc -l").split()[0]
print("Test row size:")
print(row)
matrix_tmp_test = np.zeros((int(row),col), dtype=np.int64)
print("Test matrix size:")
print(matrix_tmp_test.size)
What I am asking is, after writing to a file : $file in php, how can I then pass this file to replace:
row = run('cat '+'/Users/minks/Documents/X-test.txt'+" | wc -l").split()[0]
where the path gets replace by $file and the processing continues? Also, is it possible to pass $file directly to the python code via command line? I am little confused on how this entire passing and processing can be carried out.
Dow you want something like this?
PHP:
$path = "my.txt";
system("python predict.py $path");
Python:
row = run("cat %s | wc -l" % sys.argv[1]).split()[0]

exec not working or executing crontab script

Basically I have developed a small script that adds a cron job into a file called "crontask" and then I want to execute it so then it becomes a cron job. Here is the script:
<?php
$filename = "../../tmp/crontask.txt";
$output = shell_exec('crontab -l');
$something = file_put_contents($filename, $output.'* * * * * NEW_CRON'.PHP_EOL);
$cngDir = chdir('../../tmp/');
echo exec('crontab ' . getcwd() . '/crontask.txt');
//var_dump($exe);
?>
Everything is ok, the path is the same and if I copy and paste the path that prints out IT will carry out the cronjob but in PHP it won't???
Everything works, apart from the exec function, it doesn't execute it. Any ideas?
In terminal, if I do:
string(25) "crontab /tmp/crontask.txt"
it will execute it.
Try the following things:
Call to the command using the full command path. Sometimes $PATH is not set in the script environment and can't find the command if not.
Setup the working dir of the script using http://php.net/manual/en/function.chdir.php
Use the absolute path to access to the file
I've had a similar issue with cron jobs
I looked at your code and got a couple of ideas.
I used absolute paths
Here is my code:
$myFile = "/home/user/tmp/crontab.txt";
$addcron = "
0 0 * * * cronjob1 " . PHP_EOL .
"0 18 * * 1-5 conjob2 " . PHP_EOL .
"0 9 * * * cronjob3 " . PHP_EOL ;
$output = exec('crontab -l > ' . $myFile );
file_put_contents($myFile,$addcron ,FILE_APPEND);
echo exec('crontab ' . $myFile );
echo "<h3>Cron job successfully added!</h3>";enter code here
basically i wrote the list of previous cronjobs to a file and then appended the file with new cronjobs. Then added the new list of cronjobs with the crontab command.
the linux write to file command ' > ' was what did the trick ;)

Categories