How to start / stop an application on linux from a php file?

I want to get access to edit any txt file (in the gedit editor) from PHP.
I am trying to use something like:

<?php
shell_exec("gedit filename.txt");
?>

But he does not even give any way out:

$output=shell_exec("gedit filename.txt");
echo=echo"<pre>$output</pre>";

Is it possible to open any file or application from PHP on Linux?

+4
source share
3 answers

shell_exec- Run the command through the shell and return the full output as a string. The output is output to the terminal.

So this is:

<?php
    $output = shell_exec('ls -lart');
    echo "<pre>$output</pre>";
?>

Will return ls -lart of your directory. If you do the same with the exit gedit, these will only be error messages and warnings, because gedit returns the text to the graphical interface, and not to the terminal.

, cat

<?php
     $output = shell_exec('cat ' . $filename');
     echo "<pre>$output</pre>";
?>

.

<?php
    $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("webdictionary.txt"));
    fclose($myfile);
?>

PHP

+1

Gedit - gui.

,

// instead of less you could also use cat
$file_content = shell_exec("less filename.txt");
// ...
// manipulate the data via a textarea with html and php
$new_file_content = $_POST['textarea'];
$write_to_file = shell_exec("echo \"".$new_file_content."\" > filename.txt");
+1

First, make sure that shell_execPHP is enabled in your configuration ( here ) and that your Apache user has sufficient privileges to run applications ( here ).

In addition, for Apache to run graphical tools on the system, you must determine which display it should use; you can add this line to your Apache initial files:

export DISPLAY=:0.0

or instead add it to your PHP script:

<?php
shell_exec("DISPLAY=:0; gedit filename.txt");
?>
0
source

All Articles