3

I'd like to access for editing any txt file (on gedit editor) from PHP.
I'm trying to use something like:

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

But it doesn't even give any outputs:

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

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

dryhay
  • 79
  • 10

3 Answers3

1

Gedit is imho an editor for the gui.

What you could do is the following

// 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");
derdeagle
  • 166
  • 4
  • I want to get new gedit GUI (not another browser window or tab) with editable filename.txt inside. – dryhay Jul 12 '15 at 07:23
  • 1
    If you find a way to get the gedit GUI via PHP please let me know ;-) With the way I posted you neither get a new browser window nor a new tab. You'd just need a textarea in HTML where you put the $file_content in and edit it. – derdeagle Jul 12 '15 at 07:43
  • I don't want gedit inside browser, but new instance of gedit outside of browser (with editable filename.txt open). – dryhay Jul 12 '15 at 08:01
0

shell_exec- Execute command via shell and return the complete output as a string. Output is output in terminal.

So this:

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

Will return ls -lart of your directory. If you do the same with gedit output would be just error messages and warnings because gedit returns text into GUI not into terminal.

If you want to get some text with this command you can do it with cat

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

This is how you open and edit file.

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

PHP References

magic-sudo
  • 1,206
  • 9
  • 15
  • I don't want to get some text with this command, but I want to open gedit with editable file inside, so shell_exec is probably wrong command. – dryhay Jul 12 '15 at 07:21
0

First make sure shell_exec is enabled in your PHP configuration (here) and that your Apache user has enough privileges to execute applications (here).

Furthermore for Apache in order to run graphical tools in system, you got to define which display it should use; you can whether add this line in your Apache start-up files:

export DISPLAY=:0.0

or instead add it to your PHP script:

<?php
shell_exec("DISPLAY=:0; gedit filename.txt");
?>
Community
  • 1
  • 1
hatef
  • 5,491
  • 30
  • 43
  • 46