Run a batch file from php code - php

i have a batch file that contains a command to record a live stream from server, i have tried it in CMD.exe, it work fine.
I need now to run this file from php code (by a button "Record"), but i don't get any output.
please can you check my code and try to give me a solution please :
1 - the batch file code :
#echo off
vlc input stream --sout "#duplicate{dst=display,dst=std{access=file,mux=mov,dst=output file.mp4}"
#echo off
2 - the php file code:
<?php
$file = file_get_contents("C:\\wamp\\www\\CMD\\filebat.bat");
echo exec($file);
echo "Done!";
?>
3 - Another php code:
<?php
$file = file_get_contents("C:\\wamp\\www\\CMD\\filebat.bat");
$output = exec($file);
print_r($output);
?>
4 - Another php code :
<?php
echo exec('C:\\wamp\\www\\CMD\\filebat.bat');
?>
i tried these 3 php code but i don't get any output.
kindly, can you help me to find the best solution how i Run a batch file from php code ??
regards,

4.) Should work. Check php.ini/phpinfo if SAFE_MODE is OFF.
http://www.php.net/manual/en/function.exec.php

Related

PHP doesn't save to files

I have Apache 2 and PHP installed on a Raspberry Pi 1 B+ (RASPBIAN STRETCH LITE
). I have a website running on it with a textbox and a PHP script that is supposed to save the contents of the textbox to a text file on the server when the user submits.
I tryed basically everything but the php script just wont save.
PHP does get the textbox-content (I tested it - it works just fine).
This is my PHP:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if ($_REQUEST) {
$input = $_REQUEST["search"];
file_put_contents($dir, $input, FILE_APPEND);
}
?>
PHP is working properly aside from this problem.
The folder has read-write permissions for everyone.
I have also tryed to let PHP create its own folder with code similar to this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
PHP can't even do that.
Thanks in advance for any help!
I figured it out. The problem was that the webserver didn't have permission to write to the directory. problem solved by running sudo chown -R www-data var/www/html/data thanks for the help! Have a nice day :D
First just try this separately from other code
<?php
file_put_contents("/data/searches.txt", "bla", FILE_APPEND);
?>
if it's not working try to get a last error
echo print_r( error_get_last ( ), true)
In order to make sure that you can write to the file you can always check if it's writable
if(is_writable($dir))
Also for debugging it's nice to see how much bites was written by file_put_contents
so the final code easy to debug will be like this:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if(!is_writable($dir)){
echo "cannot write to file";
}
if (!empty($_REQUEST["search"])) {
$input = $_REQUEST["search"];
$bitecount = file_put_contents($dir, $input, FILE_APPEND);
}
echo $bitecount . " bites were written to the file";
?>
There is a thing. If $_REQUEST["search"]="" or $_REQUEST["search"]=null the if($_REQUEST) will be TRUE anyway

Problems connecting Scilab with php?

I try to use/call Scilab with php. I followed instruction form https://www.ibm.com/developerworks/library/os-php-scilab/os-php-scilab-pdf.pdf but I cant make it work. I tried listing 1 and what I got is Array (). do I need to save sci/sce file in root folder before calling them in php? is Scilab can be called directly as the instruction?
I had exactly the same problem. I use scilab 6.0.1
This code works for me :
<?php
$path = 'C:\\wamp64\\apps\\scilab-6.0.1\\bin\\';
$path_script = 'C:\\wamp64\\www\\tests\\log.sce';
$command = $path.'WScilex-cli.exe -nb -f '.$path_script;
echo $command;
exec($command, $output);
echo '<pre>';
print_r ($output);
echo '</pre>';
?>
In the log.sce file, I saw matrix on firefox web page, save file and picture on my folder.
Best regards

Save the console text into a txt file? (PHP)

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.

PHP using ruby returning output (using metainspector)

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

Problems executing Perl scripts from PHP

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

Categories