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.
Related
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.
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 />";
}
}
?>
i have php file that run from cmd matlab function -> that function is create .txt file and fill it up with the analysis results. then the php file takes this .txt file and sending the information as a string(lines from .txt) to a database (phpmyadmin).
my problem is that the php start to send the info from the .txt while the matlab is still writing to the file.
i thought to solve it with a global var that matlab and php know him, and use it as a flag that Flag determines when Matlab finished building the necessary file. i thought to use the window registry, but it is very complicated. there is any easier way?
thanks alot,
doron
my php file:
unlink('test.txt');
if(isset($_POST['filepath'])) {
$filename = $_POST['filepath'];
$inputDir = "C:\\xampp\\htdocs\\login";
$outputDir = "C:\\xampp\\htdocs\\login";
// here php open the matlab function from the cmd:
$command = "matlab -sd ".$inputDir." -r phpcreatefile2('".$outputDir."\\".$filename.".txt')";
exec($command);
$fileLoc= "".$outputDir."\\".$filename.".txt" ;
echo $fileLoc ;
echo "The following command was run: ".$command."<br/>";
echo $filename." was created in ".$outputDir."<br/>";
echo " Now the txt file will write in the DB <br/>";
// here i tried to check if the file exists and if it is not empty. but its not working because matlab still writing to the file.
while (1) {
if (file_exists("test.txt") ){
echo "check1";
if (filesize("test.txt")!= 0) {
echo "check2";
$file = file('test.txt');
$sql = "INSERT INTO `ID_5525_Medical_record`(`Data`,`AV_Power`,`Highest_Amp`,`90BW`,`Url_figure`) VALUES ('$file[0]','$file[1]','$file[2]','$file[3]','$file[4]')" ;
if(mysqli_query($connection,$sql))
{
unlink('test.txt');
echo "the txt file is now in the DB <br/>";
}
break;
}
else {
echo "i am going to sleep";
sleep(1);
echo "i am awake";
}
}
}
`
Normally, using COM would be the proper way to implement inter process communication between php and MATLAB (Or maybe some alternative, think all are sufficient for this simple task).
Here it seems you call MATLAB only one, you can fix the behaviour appending -wait to the command. This forces launcher to stay open until matlab closes, blocking your PHP-Script at exec($command);. Big disadvantage is, that you have to start Matlab for every function call and close it afterwards. With com one instance can stay open and do everything.
I have a script which when compiled from the terminal passing the parameters something like this "php compile.php arg1 arg2 arg3 arg4" on the terminal starts displaying the output result which is something like 1)file created 2) file inclusion. etc.
Now I am trying to use system(php compile.php arg1 arg2 arg3 arg4) from another php file, but it will not execute the php file or will not show an output.
For example i even tried to create hello.php------
and tried using system(php hello.php); but it did not output anything on the browser.
Can anyone please help me out, I am new to php. Thanks.
make sure you react on errors from the system call. Your actual code might also help. The following example works without problems on my box.
<?php
$file = fopen('/tmp/written.php', 'w');
fwrite($file, '<?php echo "Hello from written.php\n"; ?>');
fclose($file);
echo "calling written.php\n";
if (!system('php /tmp/written.php'))
die("something wrong\n");
else
exit("all good\n");
?>
I am uploading a video, which is supposed to generate three screenshot thumbnails. I have the same upload code running in both admin and front-end, but for some odd reason the thumb is only being generated when I upload from front end, and not from backend...
My directory structure
root/convert.php (this is the file running through exec call)
(the following two files are the upload files running in user-end and admin-end respectively)
root/upload.php
root/siteadmin/modules/videos/edit.php
I believe convert.php is not being run from admin-side for some reason. The command is something like:
$cmd = $cgi . $config['phppath']. ' ' .$config['BASE_DIR']. '/convert.php ' .$vdoname. ' ' .$vid. ' ' .$ff;echo $cmd;die;
exec($cmd. '>/dev/null &');
And echoing out the exec $cmd, I get this:
/usr/bin/php /home/testsite/public_html/dev/convert.php 1272.mp4 1272 /home/testsite/public_html/dev/video/1272.mp4
How do I make sure convert.php is being run?
EDIT: OK, now I am sure it is not being executed from admin-side, any ideas why?
http://php.net/manual/en/function.exec.php
"return_var" - If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Another way to determine if exec actually runs the convert.php file, add some debugging info in convert.php (e.g. write something to a file when the covert.php script starts).
Just an Idea
you could print "TRUE" in the convert script when it runs successfully.
don't add >/dev/null &
check the return value of exec
$value = exec($cmd);
if($value == 'TRUE')
// did run sucessfully
}
chmod 755 convet.php
you also make sure the first line of convert.php is:
#!/usr/bin/php
check the full path of php cli executable.
Also make sure convert.php las unix line ending ("\n")