PHP - My Exec Function In Php Is Not Working In Centos
I have a username alam for the server I have sudo access to execute linux commands. I could configure almost all files the root can do.
I wrote this script on the server for test as I need to provide the server some functionality ----------------- Code: [Select] <?php $cmd="ls -l"; echo exec($cmd,$ret_value) . "<br />DONE"; echo "<pre>".shell_exec($cmd)."</pre>DONE"; ?> ------------------ I get the OUTPUT as follows - ------------------ DONE DONE -------------------- I am novice user of Linux however any kind of help will be appreciated .......... Similar TutorialsHi, I have a java file that needs to be executed from my php script, the command I have in it works perfectly when I execute directly in the command line, but when I execute it from php it returns this: array(0) { } I have tested that the exec works by using antiword, which does return results as expected, but its not working for the code below. But I would much rather use apache tika. Also your thoughts on whether this is good practice please. I am using this to extract text from CV's and store that in the db to be able to search through. I need to extract from: pdf, doc, rtf and docx. this is the only tool I have found that can handle them all. your help on the exec problem and any suggestions will be much appreciated. Code: [Select] <?php exec('java -jar /etc/tika-app-1.0.jar -t /var/www/html/cvdatabase/400001.doc', $output); var_dump($output); ?> I've got a mailing list (uses DadaMail) which provides a perl script (subscribe_email.pl) so that I can pass subscribers to it thorugh a PHP script. Info he http://dadamailproject.com/support/documentation-5_0_1/COOKBOOK-subscriptions.pod.html#command_line_utility___subscribe_email_pl The trouble is I can get it working when variables are passed to it via URL query strings, but when I try to hard-code variables into it it stops working. I can't see any reason for this, it's got me really confused. This works: - Code: [Select] <?php // example usage: - // http://www.domain.com/ds1.php?sendto=user@domain.com&title=Mr&forename=John&surname=Doe&company=None&country=UK if (isset($_GET['sendto'])) { $email = $_GET['sendto']; } if (isset($_GET['title'])) { $title = preg_replace("/[^A-Za-z ]/", "", $_GET['title']); } if (isset($_GET['forename'])) { $forename = preg_replace("/[^A-Za-z\-. ]/", "", $_GET['forename']); } if (isset($_GET['surname'])) { $surname = preg_replace("/[^A-Za-z\-. ]/", "", $_GET['surname']); } if (isset($_GET['company'])) { $company = preg_replace("/[^A-Za-z()\-.& ]/", "", $_GET['company']); $company = str_replace('&', 'and', $company); } if (isset($_GET['country'])) { $country = preg_replace("/[^A-Za-z\-.& ]/", "", $_GET['country']); $country = str_replace('&', 'and', $country); } if (isset($email)) { $exec_command = 'perl ./subscribe_email.pl --list mylist --email '. $email; if (isset($title)) $exec_command .= ' --fields title '. $title; if (isset($forename)) $exec_command .= ' --fields forename '. $forename; if (isset($surname)) $exec_command .= ' --fields surname '. $surname; if (isset($company)) $exec_command .= ' --fields company '. $company; if (isset($country)) $exec_command .= ' --fields country '. $country; exec(escapeshellcmd($exec_command)); } else { echo 'Nothing to do'; } ?> The mail address and all the parameters are passed to the subscribe_email.pl script. But, this doesn't work: - Code: [Select] <?php $subscriber_email = 'user@domain.com'; $subscriber_title = 'Mr'; $subscriber_forename = 'John'; $subscriber_surname = 'Doe'; $subscriber_company = 'None'; $subscriber_country = 'UK'; if (isset($subscriber_email)) { $email = escapeshellcmd($subscriber_email); } if (isset($subscriber_title)) { $title = preg_replace("/[^A-Za-z ]/", "", $subscriber_title); } if (isset($subscriber_forename)) { $forename = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_forename); } if (isset($subscriber_surname)) { $surname = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_surname); } if (isset($subscriber_company)) { $company = preg_replace("/[^A-Za-z()\-. ]/", "", $subscriber_company); $company = str_replace('&', 'and', $company); } if (isset($subscriber_country)) { $country = preg_replace("/[^A-Za-z\-. ]/", "", $subscriber_country); $country = str_replace('&', 'and', $country); } if (isset($email)) { $exec_command = 'perl ./subscribe_email.pl --list mylist --email '. $email; if (isset($title)) $exec_command .= ' --fields title '. $title; if (isset($forename)) $exec_command .= ' --fields forename '. $forename; if (isset($surname)) $exec_command .= ' --fields surname '. $surname; if (isset($company)) $exec_command .= ' --fields company '. $company; if (isset($country)) $exec_command .= ' --fields country '. $country; exec(escapeshellcmd($exec_command)); } else { echo 'Nothing to do'; } ?> In the second example, only the list and email parameters are passed, all the others are lost (or ignored). All that's different between the two is the way the variables are inititally set, I can't for the life of me figure out why the second one isn't working . Even stranger, I tried testing with just: - Code: [Select] <?php exec("perl ./subscribe_email.pl --list mylist --email user@domain.com --fields title Mr --fields forename John--fields surname Doe --fields company None --fields country UK") ?> and this doesn't work fully either (again just the list and email parameters are passed). Unfortunately, it's the second method I need to use as it's tagged on to the end of a contact form processing script which initiates the subscription if a checkbox is left ticked...and all variables will be coming from this form script. Any help greatly appreciated! Hi I'm trying to resize pictures for thumbnails I can do this correctly at linux command line with the following command convert ideologies.jpeg -resize 300x200 ./Thumbnails/testphp.jpg However trying to wrap this in PHP doesn't seem to work // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; exec("convert {['fileName']} -resize 300x200 wankertestphp.jpg"); } If you look above I do successfully hit the "The File has been uploaded successfully" line so it is entering that If statement. I've also checked the Threads table with a Select * from Threads and I can see the $fileName variable is entering there. I've tried numerous different ways around the $fileName variable (removing the brackets...removing the brackets and the apostrophes")
Any help is appreciated. Hmm, hello everyone, i'm running my php in an ubuntu server and I'm trying to run a command using exec (i.e. 'echo 1 > /var/www/test.txt') but I always get a permission denied issue, although i've already changed the permissions to 777 (read-write-execute) Any solution? hi guys. i'm new to php and Apache and i have a problem. i'm trying to execute an exe file that i have in my folder, and the code i use is Code: [Select] echo exec('test.exe');i have tried using this with and without the ".exe." and still no luck. i have also tried using a different path Code: [Select] echo exec('C:\Windows\System32\mspaint');now these codes work when i run them from my php editor(PhpED), bun when i try running it on Apache it gets stuck. (just shows the web page as loading with no end. please help me out. i'm guessing it something with security. also if there is any other code i can use please let me know (i have also tried using system) Hi, I'm trying to build a 'simple' page to improve a process where I work.
I'm wanting to do this.
Read either the last line, the last 10 lines, or something I decide on from a log and display the output in table rows and columns.
I've done this before with loops and db query's, but don't know how to approach it using php only as this is the first time I'm using it.
So right now I have this in a table but it will only display one row.
<tr><td colspan="5"> how do i enable exec() in php.ini? where is php.ini? cant find it in the directories in my site. I have a PHP script containing the function exec which works fine in PHP 4 in IIS 7 on Windows 7. However this function does not work in PHP 4 in IIS 5 on windows server 2003. But I have to make it work there because that is what we have on our web server. Can someone provide a solution? Hey guys I usually code in C and I started looking into PHP which I think is pretty good. I'm wondering if there's a function that can be used to execute a command from a remote server on my local machine??? As example if I'd like to have a PHP page/script running on my server which when logged onto would display me a page with buttons like "PING, NETSTAT, IPCONFIG, TRACERT" and when clicked on it would exec those commands but not on the server but from my machine??? So that I would get the PING from my local machine to google.com instead of from the server to google.com ??? making any sense ??? thanks i m using code for sending email through GMAIL SMTP as given below... can any one tell me that how i can send EMAIL through Centos 5 CLI mode.... i which folder places i save my PHP files or any other supporting files etc. in graphical mode we use webbrowser but in Command line how i can use this project.... and also please tell me how i can run firefox in CLI mode..... i m a new user of PHP and linux, please guide me in easy way. <html> <head> <title>PHPMailer - SMTP (Gmail) advanced test</title> </head> <body> <?php require_once('/var/www/PHPMailer_v5.1/class.phpmailer.php'); //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch $mail->IsSMTP(); // telling the class to use SMTP try { $mail->Host = "smtp.gmail.com"; // SMTP server $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "john@gmail.com"; // GMAIL username $mail->Password = "password"; // GMAIL password //$mail->AddReplyTo('name@yourdomain.com', 'First Last'); //$mail->AddAddress('user1@gmail.com', 'user1'); //Set Send To Address $mail->AddAddress('receiveremail@gmail.com', 'user2'); //Set Send To Address $mail->SetFrom('senderemail@gmail.com', 'senderemail Name'); // Set Send From Address //$mail->AddReplyTo('name@yourdomain.com', 'First Last'); $mail->Subject = 'Email with Attachment with PHP is done'; $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically $mail->MsgHTML(file_get_contents('contents.html')); $mail->AddAttachment('/images/asdf.pdf'); // attachment $mail->AddAttachment('/images/asd.wav'); // attachment $mail->Send(); echo "Message Sent OK</p>\n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! } ?> </body> </html> Hi guys! I have a php script that works and outputs info: <?php echo exec("uptime"); ?> and another that doesn't output anything: <?php echo exec("MP4Box"); ?> Why? Hello, I was just seeing if someone could push me in the right direction. I have a form that uploads items to a database and it also includes uploading images to a directory as a part of that. That all works, but I was thinking for security it'd be best not to leave that directory as 0777 for permissions. In researching this I found the exec() function. I'm still figuring out the relation of who the permissions are associated with and why. All I know is if the directory has 0777 for permissions my script works just fine. I was just seeing if using exec to change the permissions during the process of uploading the images and then change back would be cumbersome and leave open other problems (there won't be any user-based input being passed to that) or if that actually would be the way to go for this. Thanks! -Frank
I figure I’m doing something basic wrong.
$cmd = “at $wtime $wdate <<< “sftp -a -r -P xxx xxx@xxx.xxx.net:”.$file.” /mnt/TRFR” ; the “at” command is never generated atq returns blank. But if I copy the output of the echo from the web page into bash the “at” job gets created.
What am I missing here? Edited April 9, 2020 by Henry_Whoi have a bash script that i was running with cron jobs: #! /bin/bash start=1000 homedir="/home/alin/NetBeansProjects/Fragger2" numberofprocess=`expr $start + $2` i=$start currentnumber=`ps ax | grep index | grep -c php` case "$1" in start) while [ $i -lt $numberofprocess ] do if [ `ps axo cmd | grep index | grep php | grep -c $i` -lt 1 ] ; then /server/php/bin/php $homedir/index.php $i & fi; i=`expr $i + 1` done ;; stop) while [ $currentnumber -ne 0 ] do kill -9 `ps ax | grep php | grep index | awk {'print $1'} | head -n 1`; currentnumber=`ps ax | grep index | grep -c php` done ;; *) echo "Usage : " $0 "start|stop" ;; esac exit 0 and it was working fine, now i'm tring to make it run from a page . if(isset ($_POST['stop'])){ $conn->update("update `links` set `proccess`=0 "); exec("/home/alin/NetBeansProjects/Fragger2/Trans stop 0"); } if(isset ($_POST['start'])){ if(!preg_match("/^[0-9]+$/", $_POST['proccess'])) echo "proccess should be only a number!"; exec("/home/alin/NetBeansProjects/Fragger2/Trans start ".$_POST['proccess']); } and i don't know why it is not working, i have set for this folder all the permision for this folder "chmod -R 777 /home/alin" it doesn't stop running and i can't figure out why i have to kill the procces from the comand line to stop I'm trying to open a batch file in php with these contents: Code: [Select] @echo off java -classpath rscd.jar;lib/mina.jar;lib/xpp3.jar;lib/slf4j.jar;lib/xstream.jar;lib/hex-string.jar; org.rscdaemon.server.Server pause My PHP code is simply Code: [Select] $run = "C:\\Users\\Zorian\\Desktop\\EasyRSC\\Server\\run-win.bat"; echo exec("cmd.exe /c " . $run); It should open up a java applet, instead it just echos: Code: [Select] Press any key to continue . . . Hi all.
I tried to execute command below Hello i am trying to execute a linux program for every line of a textarea Code: [Select] $text = $_POST['links']; $text = htmlspecialchars($_POST['links']); $text = nl2br($text); foreach(explode("\n",$text) as $row) { $result = NULL; $command = 'the command'; exec($command,&$result); echo $result[0]."<br>"; } but this always print the result for last line of text area for example if there are 3 lines it print 2 blanks and 1 result... whats wrong?? I would like PHP to kick off the following command: Code: [Select] arp -a|sed 's/ /,/g'>arp.csv I also have an executable makeArpFile.sh: Code: [Select] #!/bin/bash arp -a|sed 's/ /,/g'>arp.csv And I have tried executing it in php with the following: $arp = exec("bash makeArpFile.sh", $output, $exit_code); echo("Last line: " . $arp . "\n"); echo("Output: " . $output . "\n"); echo("Exit Code: " . $exit_code . "\n"); /*returns: Last line: Output: Exit Code: 127 */ I also tried just plain old: $arp = exec("makeArpFile.sh", $output, $exit_code); echo("Last line: " . $arp . "\n"); echo("Output: " . $output . "\n"); echo("Exit Code: " . $exit_code . "\n"); /*returns: Last line: Output: Exit Code: 127 */ I can run plain old arp>arp.txt but I really need it formatted in BSD style (arp -a>arp.txt) in order to be able to parse it properly later. Any thoughts? EDIT: for fun I made a useBash.sh script that executed "bash makeArpFile.sh" still no luck. have a executable binary to which i pass some dynamic values as hostname,host id,server name,server id and a text field value. Now i call this binary using exec or shell_exec functions in PHP to stimulate Command executions as a admin. But i am able to invoke the binary and not able to use psexec service of windows. I get logs and DB entries successfully but i also have a xml push operation in psexec call that is required. the format is like :: exec($url,$output,$return_var); url is the command path with parameters . output is an out param and return is the code on execution this is executed in PHP and i want this binary to generate the same output as done from CLI . Currently i guess it's a permission issue with shell that disallows psexec call. How do i invoke the binary with psexec access outside PHP on command shell I have set access privilege of Powershell to "unrestricted" so that any script can be executed. Still it hangs up somewhere in th script causing problems in copying XML file. Powershell psexec is required in the process and i need to track down the root cause so that php side scripting of binary is successfull. Please let me know about your ideas in fixing this. I am using google and trying examples ,but i havent got any feasible solution. Hope some expert advice can guide me out of this. Hey guys i am trying to create a small script using exec() to convert PDF files into images ... The script worked well in the past and for some reasons it staped working. exec('"C:\\Program Files\\ImageMagick-6.7.1-Q16\\convert.exe" -verbose -density 150 D:\\Inetpub\\www.goulet.ca\\Pub\\Web\\new\\media\\other\\'.$value.' -quality 100 -sharpen 0x1.0 D:\\Inetpub\\www.goulet.ca\\Pub\\Web\\new\\media\\pdf_flipbook\\'.$value.'\\img.png', $out); the $out var returns an empty array |