Note that there are some explanatory texts on larger screens.

plurals
  1. POphp form exec, sed and file_get_contents
    primarykey
    data
    text
    <p>I am working on a script to edit a file on my vps, so far thanks to the help from a user here I have the following:</p> <pre><code> &lt;?php if(!empty($_REQUEST['color_choice'])){ exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /home/user/colors/color.choices"); echo 'File color choice has been updated'; } ?&gt; &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;select name="color_choice"&gt;; &lt;option value="red"&gt;red&lt;/option&gt;; &lt;option value="blue"&gt;blue&lt;/option&gt;; &lt;option value="black"&gt;black&lt;/option&gt;; &lt;option value="orange"&gt;orange&lt;/option&gt;; &lt;/select&gt; &lt;input type="submit" name="Submit" value="Submit" /&gt; &lt;/form&gt; </code></pre> <p>This changes the values as needed but I have one issue I am still trying to solve. The script does not get the current value that is in the file itself, so when I first visit the page it always says "red". After I make a change it still says "red" on the php form.</p> <p>Edit: I would like the form to always display the current value in the color.choices file, instead of just going back to "red" every time. If I change the value on the form to "blue" I want the form to display that the current value in the file is set to "blue".</p> <p>I have been told I need to use "file_get_contents" to first retrieve the value from the file itself. I have made a few attempts but I am getting no where. If I need to add more information please let me know!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI would like to help you but your current question is a little unclear. Give more details about the file you need to retrieve info from. Given your current question it is hard to understand exactly what your code does (it is just a simple form and exec call). What does your application do currently, what is it supposed to do? Pre-conditions/post-conditions for submitting the form? Give us a little more details so we can help! P.S. you shouldn't have the form submit to $_SERVER['PHP_SELF'] - bad practice! --- <form method=POST> you don't need the action attr if your submitting to self.
      singulars
    2. COYou need to get the value first and then make it the default option on the form. How do you retrieve the value now? Or where is the value stored?
      singulars
    3. COMy script is just a simple form and exec call, the file itself just has the following value: color=x. I basically want the form to always reflect the current value in the file on the server. So if I change the value to blue with the form field I want it to display that the value is now set to blue on the form. Currently it just goes back to displaying "red" every time even after a change is made. Also when I come back to the page it will just say red even when the value has been changed to blue.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload