PHP - Shell_exec() Madness... [mod Edit: Was Start_exec()]
NB: oh yeah, the specs: PHP 5.5.9-1ubuntu4.4 Server version: Apache/2.4.7 Ubuntu 14.04-4
OK, this is driving me completely nuts.
Yes, the first problem is, I'm modifying someone else' code, so I'm trying to cludge this, but I don't have the time resources to write the entire application, at least not at this prototype stage.
That said, start_exec() works wonderfully in a standalone, test script, so there's nothing wrong with the server or php mods / version.
The scenario, on clicking an href or button, I want a script to run (it loads files from a remote sftp and also wgets from another server). One would think this isn't much of a problem, so here's where it becomes crazy making.
The code I'm working with is generating the html with print() in the form of print "<html>";
I've tried the usual examples for doing this function, but none of them are using embedded php in embedded html.
So this:
print"<tr><td class=leftsub><div width=100% class=info id=tasks> <a href='windows.php?pull=true'><img src=images/explore.gif width=16 height=16> <u>Pull files from server</u></a><br>does not invoke this: if ($_GET['pull']) { # This code will run if ?pull=true is set. shell_exec('pull.sh'); }Which is all in the local function. And this doesn't work either: print "<img src=images/extract.gif onClick='shell_exec('./pull.sh')' class=button title='Pull files'>";Nor does this: print ("<form method='post'><p><button name='button'>Run Perl</button></p></form>");seem to call this: if (isset($_POST['button'])) shell_exec('./pull.sh');There are other ways I could perform the same, even just a cron that pulls and pushes on some regular interval, of couse which is checking to see if the files are open before pulling or pushing, but I'd rather it be on demand. Am I just going to drive myself nuts without re-writing the entire code? Should I rather just have the requests call a different php script which then inline shell_exec()'s the shell script? Any insight and feedback is very much appreciated. Cheers, Kevin Edited by krattai, 21 September 2014 - 01:43 PM. Similar TutorialsHi there, I signed up to this web site because I've run into a problem I just can't figure out. I'm not a programmer or anything like that, so maybe that explains my inability. Let's say I have a SQLite database with two columns (Department & Category). I want to figure out through PHP and SQL which departments have what category in common. There are about 14 categories and 70 departments. I need the output to look like this: Code: [Select] {"id":"AgricultureandFood", "text":"Agriculture and Food","connections":["DepartmentforCommunities","TreasuryandFinance","ChemistryCentre"]}] Where connections are the categories the departments have in common. Currently, I have the following code that seems to get something done but not nearly what I need: $dbh = new PDO("sqlite:kpi_database.db"); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $department = "SELECT DISTINCT Department FROM Efficiency"; $department_result = $dbh->query($department); $category = "SELECT DISTINCT Category FROM Efficiency"; $category_result = $dbh->query($category); $data = array(); foreach($category_result as $category) { $link = "SELECT DISTINCT Department FROM Efficiency WHERE Category = '" . $category[0] . "'"; $link_result = $dbh->query($link); foreach($department_result as $department) { $item = array('id' => preg_replace('/\s+?/', '', $department[0]), 'text' => $department[0], 'connections' => array()); foreach($link_result as $link) { $item['connections'][] = ($_GET['multi'] == 1 ? : preg_replace('/\s+?/', '', $link[0])); } $data[] = $item; } } print json_encode($data); I think the problem is that I can't think inside of the loops properly, and I'm missing something simple. If anyone is interested, I worked out this example from the Moowheel example code (that's what I'm trying to make out of my own database). I really appreciate any help with this. Hi Guys I cant seem to get unzip to work via php shell_exec this is what I have: Code: [Select] $rworld = $_GET['restore']; $connect->call('remotetoolkit.stopServer',Array()); sleep(12); $nrworld = substr($rworld, 0,-15); $sftp = new Net_SFTP($host); $sftp->login($root_user, $root_pass); $sftp->delete('/root/minecraft/'.$nrworld.'/', true); echo shell_exec("unzip -jo /root/minecraft/saves/$rworld -d /root/minecraft/$nrworld"); sleep(5); $connect->call('remotetoolkit.startServer',Array()); Nothing is returned, it doesn't echo and the command is not executed. If I execute via terminal and change the $variables to the actual names it works fine, and I can ls with shell_exec successfully? Any ideas? Good Friday to everyone! I am writing some PHP code to find certain files and the search the files for a certain string. For example, this is what I have. This works in a LINUX shell environment, yet I need to have it work in PHP since the GUI is web based. Can anyone help? Thanks! Mike Code: [Select] <?php $output = shell_exec('find /sync/www/html/ohx/products/MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IN EFFECT" /dev/null {} \;'); echo "<pre>$output</pre>"; ?> When i try to do a shell_exec from my script the script does not work. But when i put the same thing in a .bat file and execute that from PHP it wroks. Can anyone help me shell_exec('xcopy C:\xampp\htdocs\test\backup\test.zip "\\fileserver\KT_backup\aback\" /i') Hi im Art, Im trying to make use of Shell_exec() function to execute gammu(smsgateway for linux<CLI>) to text a message using my php page. This is my actual code: <html> <form method="POST"> Message: <textarea name=Message></textarea> Number: <input type=Text name=Number> <Input type=Submit name=Send value=Send> </form> </html> <?php extract($_POST); if (isset($Send)){ $output = shell_exec("pwd"); echo $output."<br>"; $cmd="gammu --identify"; echo $cmd."<br>"; $output = shell_exec($cmd); echo $output; $cmd="echo '".$Message."'|gammu sendsms TEXT ".$Number; echo $cmd."<br>"; $output = shell_exec($cmd); echo $output; } ?> But this is the error: /opt/lampp/htdocs gammu --identify Error opening device, you don't have permissions. echo 'Hi There'|gammu sendsms TEXT +639270000000 Error opening device, you don't have permissions. PS: It works well if just issue the command echo 'Hi There'|gammu sendsms TEXT +639270000000 on my terminal but i dont know why it doesnt work for php, and another thing, i wasnt using any sudo commands on shell i just type the command directly, hope someone can help me out thanks This topic has been moved to Other Web Server Software. http://www.phpfreaks.com/forums/index.php?topic=344940.0 I'm trying to call a command using shell_exec by doing the following- Quote shell_exec("C:\\myprogram.exe -t")or die('error'); but that doesn't work, as it returns 'error'. The command "C:\\myprogram.exe -t" works perfect when i call it myself using the cmd.exe but not when i try using it in php with shell_exec. Safe mode is off and i'm using apache as my php webserver. Does anyone know what the problem is here and if not then is there any other ways of executing myprogram.exe in php? In the browser when I open the file I need to see the output of the ifconfig linux command on the server. This is the index.php <? $iplist = shell_exec("ifconfig"); print "<pre>$iplist</pre>"; ?> but when I open the index.php in a browser I can see only the first couple of lines, nothing more. Can someone help me what to do? The strange thing is when I run the index.php from the command line with "php index.html" it shows the correct result. Any help..? Thanks, I am using shell_exec, and it runs a ping on a server, is there any way to flush the buffer of the shell_exec as the data is getting written to the buffer? Because I sometimes ping a site 10 times and nothing get written until the function finishes, so is there anyway to write out the output as soon as it is available? btw, this isn't only for doing a ping it is for other things as well. Hi guys, I'm running the following code: Code: [Select] $result = shell_exec("su user; scp /path/to/file/text.txt user@domain:/path/to/file/text.txt"); Now, the file get's transferred correctly. However, I need to put in place a check to check if the transfer was successful. $result seems to return the same whether the transfer was successful or not. I want to send an email to state whether the file transfer was successful so what do I need to do to check this? Thanks! Hello all, I am using a shell_exec command like the one below: $output = shell_exec('c:\powershell\powershell.exe get-service'); echo($output); However, when launching the page, the browser will hang with the hour-glass. If i end the powershell.exe process on the web server via task manager, the browser then returns the expected output. Any idea why this is? Other commands, like ipconfig return the correct output and the browser displays "Done" in the bottom left. Any help appreciated. Andrew Debian 5 PHP 5.2.6-1 This script works: <? $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> This script doesn't work: <? $vars=unserialize(file_get_contents("file.txt")); $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> I get error: Quote Warning: shell_exec(): Unable to execute 'ls -lart' or Warning: system(): Unable to fork [ls -lart] If file.txt has size 500k, the script works without errors. But my file file.txt has size 30M. Help. if(isset($_POST['clearflags'])){ $output = shell_exec('php clearflags.php'); } The above works fine on my local development server running Debian 10, PHP 7.4 and Apache 2.4.25. clearflags.php is a script that clears various fields in a MySQL database so it's easy to determine if it properly ran or not. On my hosted server however (Bluehost), clearflags.php is never executed. My hosting service swears that SAFE MODE is not on in PHP (which is also Version 7.4). If I call from the command line (php clearflags.php), it runs fine. $output on the hosted server is "Content-type: text/html; charset=UTF-8 " but nothing on my local server where the script is called and runs OK. No entry appears in the error log about this I've also tried using exec() with the same failed result Running phpinfo() on the hosted server shows that disabled_functions are "no value" Thoughts? Edited October 11, 2020 by KenHorsehello mates, I am needing help on editing a line in Flat File Database. I got it to pull the data i need out by using id but not sure on how i would go about getting it to update the FFD heres what i got so far $id2 = $_REQUEST['id']; $id = array(); $username = array(); $title = array(); $date = array(); $message = array(); $lines = file('../news/news.db'); foreach($lines as $line){ if($id != $id2) { $explode = explode("||",$line); $id = $explode[0]; $username = $explode[1]; $title = $explode[2]; $date = $explode[3]; $message = $explode[4]; } } ?> <form action="edit_news2.php?id=<?php echo $id; ?>" method="post" name="editnews"> <table width="100%"> <tr><td><input name="id" value="<?php echo "$id;" ?>" type="hidden" />UserName:</td><td> <input name="userName" value="<?php echo "$username" ?>" type="text" readonly="true" /></td></tr> <tr><td>News Title:</td><td> <input name="title" type="text" value="<?php echo "$title"; ?>" /></td></tr> <tr><td>Date/Time:</td><td> <input name="datetime" value="<?php echo "$date"; ?>" type="text" readonly="true" /></td></tr> <tr><td colspan="2">Message:</td></tr> <tr><td colspan="2"><textarea name="message" cols="50" rows="15"><?php echo "$message"; ?></textarea></td></tr> <tr><td colspan="2" align="center"><input type="submit" name="submitBtn" value="Post News" /></td></tr> </table> </form> thank you Valkeri2010 I'm using php edit function to edit an php file online. I think the portion that writing the file is below. My issue is that wherever there's a quote ("), it's automatically adding a slash (\). For example, if I had class="abc", it changes to class=\"abc\". How do I fix this? Code: [Select] <?php $file = $_GET['f']; $script = $_POST['script']; if($file&&$script) { $fp=fopen($file, "w"); fwrite($fp,$script); fclose($fp); } ?> hey guys i have an array but want to add "n." infront of each key value...example n.news_id, n.user_id etc...is there a simple way/function of doing it other than changing the values through a loop? Array ( [news] => Array ( => news_id [1] => user_id [2] => news [3] => date [4] => time ) ) Hi all, I don't know if it is possible but i need to edit one word document, write the name of the customer in it, but at the right place and print it from a php form. The user write in that php form the customer id and if he exists, the system should modify the word template and print it. Do you think this is possible and if so how can i do that? Thanks in advance. Hugo ok I have text files with my logins to my emails and such.. Well I have this script to prepare the logins for another script the files with the logins are in this format email pass email pass email pass email pass $filename = 'text.txt'; $file = file($filename); $begin = "$"; foreach($file as $b){ list($email,$password) = explode(" ",$b); $emails[] = $begin."header[]=get_header('".$email."','".$password."');";} file_put_contents($filename,implode($emails)); echo "Text file edited"; sleep(5); Is what I am using to get the email and password in this format $header[]=get_header('email','pass'); but it is putting it like this $header[]=get_header('email','pass '); $header[]=get_header('email','pass '); $header[]=get_header('email','pass '); $header[]=get_header('email','pass '); $header[]=get_header('email','pass Why is it putting '); on a new line? I need it to be right behind the password so it looks like this $header[]=get_header('email','pass'); $header[]=get_header('email','pass'); $header[]=get_header('email','pass'); $header[]=get_header('email','pass'); $header[]=get_header('email','pass'); any ideas? Thanks Ok I have a file with my emails and passwords like this email:pass email:pass email:pass email:pass and so on.. Is it possible to make a script that will delete everything after the ":" and replace the ":" with a comma? Im not sure what functions or w/e I could use to make this possible? Any ideas? Thanks Hi can anyone please help me, I have to create a edit page that retrieves information from a list so that a user can change the text. The page looks fine but when I enter data in the fields and update it leaves the page as it was retrieved. I am a beginner! My code looks like this: Code: [Select] <form method = "get" action = "edit.php"> <?php //check to see if user is logged on session_start(); if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) { header ("Location:login.php"); } include('connect.php'); //connection details to database in a connect.php page $product_id = $_GET['product_id']; $query = "SELECT * FROM products WHERE product_id = '$product_id'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $product_id = $row['product_id']; echo "<table>"; echo "<tr>"; echo "<td><input name = 'product_id' type = 'hidden' value ='$row[product_id]'></td>"; echo "</tr>"; echo "<tr>"; echo "<td>Product Name:</td><td><input name = 'pname' type = 'text' value ='$row[product_name]'></td>"; echo "</tr>"; echo "<tr>"; echo "<td>Product Range:</td><td><input name = 'prange' type = 'text' value ='$row[product_range]'></td>"; echo "</tr>"; echo "<tr>"; echo "<td>Product Price:</td><td><input name = 'pprice' type = 'text' value ='$row[product_price]'></td>"; echo "</tr>"; echo "<tr>"; echo "<td><input type = 'submit' name = 'Submit' value = 'Update'></td>"; echo "</tr>"; echo "</table>"; } //if form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST'){ //get values from fields $productname = $_POST['pname']; $range = $_POST['prange']; $price1 = $_POST['pprice']; $price = (int)$price1; if ($productname == "" || $range == "" || $price == "" ) { $errorMessage .= "Please fill in all text boxes"; } else { $errorMessage = ""; } $query = "UPDATE products SET product_name = '$productname', product_range = '$range', product_price = '$price' WHERE product_id = '$product_id'"; $result = mysql_query($query); print "<br> $productname from range $range with a price of $price has been updated!"; } ?> Thank you in advance! |