title: unlink image only if exists in laravel
body: <p>While updating the user image unlink image from public folder if exists otherwise do update the user with image. Currently I have no image for user. And while updating user from profile section I am getting this error <strong>unlink('images/users') is a directory</strong>. I want if image exists for user then unlink the image and upload the new one otherwise just upload the new image.</p>
  <p>My controller:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public function changeUserImage(Request $request)
      {
          $this-&gt;validate($request, [
              'image' =&gt; 'required|mimes:jpeg,jpg,png|max:10000',
          ]);
          $image = $request-&gt;file('image');
          if (isset($image)) {
              $imageName = time() . '.' . $request-&gt;image-&gt;getClientOriginalExtension();
              if (!file_exists('images/users')) {
                  mkdir('images/users', 0777, true);
              }
              if (file_exists('images/users')){
                  unlink('images/users/' . \auth()-&gt;user()-&gt;image);
                  $image-&gt;move('images/users', $imageName);
                  User::find(\auth()-&gt;user()-&gt;id)-&gt;update(['image'=&gt;$imageName]);
              }else if (!file_exists('images/users')){
                  $image-&gt;move('images/users', $imageName);
                  User::find(\auth()-&gt;user()-&gt;id)-&gt;update(['image'=&gt;$imageName]);
              }
          }
          return redirect()-&gt;back();
      }</code></pre>
  </div>
  </div>
  </p>