title: How can I make file editing conditional?
body: <p>Here is my code:</p>
  
  <pre><code>$file = '/etc/apache2/sites-available/000-default.conf';
  // Laravel Projects
  $newDirectory = "&lt;Directory $path/public&gt;
                       Allow Override all
                       Require all granted
                    &lt;/Directory&gt;";
  $virtualHost = file_put_contents($file,str_replace("&lt;/VirtualHost&gt;","$newDirectory\n\n&lt;/VirtualHost&gt;",file_get_contents($file)));
  </code></pre>
  
  <p>It works as well. As you can see, it opens <code>000-default.conf</code> file <em>(in linux)</em> and appends a new directory at the end of <code>&lt;VirtualHost&gt;</code> block. </p>
  
  <p>My problem is, sometimes the rest of the code throws an error and when I run the whole script again, code above will append that directory twice. </p>
  
  <p>How can I add a condition on the way of <code>file_put_contents</code> to check first if if the same directory isn't exist, then append it, otherwise don't do anything?</p>