I am trying to integrate matlab with php. So i need to use php to call a matlab function. I am passing file path as argument to the matlab function using exec(). I tried lot of things but couldn't make it work. This is my simple code of php to call function phpcreatefile() in matlab.
<html>
<body>
<form action="" method="post">
Enter a filename <input type="text" name="filepath"><br />
<input type="submit" /><br />
</form>
</body>
</html>
<?php
if(isset($_POST['filepath'])) {
$filename = $_POST['filepath'];
$inputDir = "C:\\matlab_2015\\bin";
$outputDir = "C:\\xampp\\htdocs\\software";
//echo $outputDir;
$command = "matlab -sd ".$inputDir." -r phpcreatefile('".$outputDir."\\".$filename."')";
exec($command);
echo "The following command was run: ".$command."<br/>";
echo $filename." was created in ".$outputDir."<br/>";
}
?>
P.S : I have already switched the safe mode to off by making safe_mode_exec_dir=Off in php.ini and restarted the xampp.
I tried the command in cmd manually and it works properly.
Related
I am trying to run a basic script using PHP. I am very beginner in PHP and I am trying to handle some data from a static website.
First, I created via VSC an HTML page which contains the following code:
suggestion.html
<form action="test.php" onsubmit="file_handler()" method='POST'>
Pseudonim: <input type="text" id="name" name='name'><br>
<div class="s">Sugestie:</div> <br><textarea maxlength="800" id="suggestion" name='text'></textarea><br>
<div class="span-ch"><span id="charNum"> </span> <span id="charText"></span></div><br>
<input type="submit" value="Submit">
</form>
The data of this form should be sent through a POST method to test.php, which contains the following code:
<!DOCTYPE html>
<html>
<?php
if(isset($_POST['name']) && isset($_POST['text'])) {
$filename = preg_replace('#[^A-Za-z0-9_-]#', '', $_POST['name']);
$file = $_SERVER['DOCUMENT_ROOT']."/textfiles/$filename.txt";
$f = fopen($file, 'w');
fwrite($f, $_POST['text']);
fclose($f);
echo 'Success.';
} else {
echo 'Error.';
}
?>
</html>
I have done the followings:
I went to windows.php.net. Downloaded the last version of PHP.
I extracted the php zip on C drive.
I installed PHP Debug, Intelephence and extension pack.
I configured PHP executablePath. I added the dict "php.executablePath": "C:\\php\\php.exe"
I run the code in my console and it seems to work.
On the browser, instead of executing the code, it simply renders it on the browser. The main point of this file is to create a text file that should be saved on server side using the input sent via POST method.
For the past two days I've been struggling with this issue. Can anybody help me understand why my php script is not ran when I submit the form?
I much appreciate your time!
I'm using PHP's exec function to execute a bash script.
<?php
if(isset($_POST['submit']))
{
$input = $_POST['submit'];
exec("sudo -u root sh /var/www/html/script.sh '$input'");
}
?>
<form action="" method="POST">
<input type="text" name="submit" value="">
</form>
As you can see, the exec command is dependent on user input. When the user's input is just one word, everything work fine and the bash script receives the input. When the user input is multiple words however, PHP does not run the exec command. The exact same command that PHP is executing works perfectly fine with multiple words when run from the command line, so it's definitely a problem with PHP.
I have just tried to test it and with your code and my script like this:
printf "$1\n" >> f.txt
it worked.
Only edition was <input type="submit"> to submit the form.
If you put 1 2 3 the result should be 1 2 3.
Check the code below
/var/www/html/script.sh
#!/bin/sh
for i in "$*"
do
echo $i
done
test.php
<?php
if(isset($_POST['submit']))
{
$input = $_POST['submit'];
print exec("sh /var/www/html/script.sh $input");
}
?>
<form action="" method="POST">
<input type="text" name="submit" value="">
</form>
I'm setting up a Apache 2, PHP 7, Python 3.6.3 on my Raspberry pi (Ubuntu)
I want execute Python script using PHP.
I've tried execute below code, but it does not work.
This is my body tag.
con_LED.php:
<body>
<form method="post">
<button name="LightON" value="on">LightON</button>
</form>
<form method="post">
<button name="LightOFF" value="off">LightOFF</button>
</form>
<p></p>
<?php
if (isset($_POST["LightON"])) {
exec("python3 /var/www/html/LEDON.py", $output, $var);
print_r($output."<br>");
print_r($var);
}
if (isset($_POST["LightOFF"])) {
exec("python3 /var/www/html/LEDOFF.py");
print_r($output);
print_r($var);
}
?>
</body>
I did some some troubleshooting.
To check, I replace my code to:
exec("ls -al", $output, $var)
Which works.
I also made:
a.py:
with open("t1.txt","a") as f:
f.write("Heelo\n")
And executed:
exec("python3 /var/www/html/a.py", $output, $var);
This also works (meaning I can see t1.txt file on my server).
My project is using con_LED.php code to turn the LED on and off.
But my code isn't working.
Of course, I also tried this code directly on shell, which is working.
Does anyone know what my problem is?
I have an account on Gator.
I am trying to run a php script as follows
;$command = escapeshellcmd('./simple.py 2>&1');
$output = shell_exec($command);
echo $output
I would like to get as output anything that the python script prints to screen, but the output is empty no matter what I try (I use standard print in python
Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
$command = escapeshellcmd('./simple.py 2>&1');
escapeshellcmd is a form of surrender, it's php stupidity, it gets used as a DWIM band-aid when the programmer has lost control of the command-line.
It's breaking your stderr redirection.
Use escapeshellarg where you need to escape shell stuff.
$command = escapeshellarg('./simple.py') . ' ' . '2>&1';
I have an overhead controller (sort of a magic wand for some robots) plugged into my usb port.
I am trying to create a web server on linux that allows users to send a preloaded code (so no one is inputting anything) to the robots. I have embedded the command in a script file that I am trying to execute in a php file. The bit of code that executes the script file in php is run when a button is pressed on the web server. However, when I press the button nothing happens. The code doesn't get sent to the controller and no errors are given.
I can run the script in the command line with no problems. But when I try to execute it run it from the web server, nothing happens. Is there a way I can resolve this?
This is what's in the php file
<html>
<body>
<h1><font face="verdana" color="green">SwarmArena</font></h1>
<hr>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type ="text" name = "name"/>. ; <br>
Age: <input type ="text" name="age"/>. ; <br>
New Code: <textarea cols=40 rows=10 name="Newcode">Default text</textar$
<input type = "submit" name="enter" value="Enter" />
<input type = "submit" name="insert" value="insert" />
<input type = "submit" name="select" value="select" />
<input type = "submit" name="sleep" value="sleep" />
<input type = "submit" name="reset" value="reset" />
</form>
</body>
</html>
<?php
if(isset($_POST['sleep'])){
$out = shell_exec('./sleep.sh');
echo "$out";
}
?>
this is what's in the sleep.sh
#!/bin/sh
cd kilocmd && ./kcmd -s /dev/ttyUSB0 -c sleep
echo "done";
Thanks
I don't know how you are doing this. But onclick the button it should submit to a php page which will run the shell scripts using exec and shell_exec php commands .
More Over that Several Possibilities May Cause Issues like
- exec and shell_exec are disabled in php.ini
- The path to the executable is wrong. If the script is in the same
directory as the php file, try exec(dirname(__FILE__) .
'/myscript.sh');
Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues.
Log into the user that the web-server is running under and try executing the command manually from there. I personally have my webserver running under a user named apache.
To view your current user run whoami in the command line.
To change user run su - newuserhere.
If this works, or you are running the web-server under root then you can most likely rule-out any permission issues.