0

I have a page that edits some text fields and also a current file stored on the server. If the user leaves the file input blank, it automatically deletes the file already stored on the server. I would like to prevent this and if user leaves it blank the file doesn't delete Here is my php script:

 $allowedExtsf = array("pdf");
      $tempf = explode(".", $_FILES["flyer"]["name"]);
      $extensionf = end($tempf);
      if (($_FILES["flyer"]["type"] == "application/pdf") && ($_FILES["flyer"]["size"] < 524288000) && in_array($extensionf, $allowedExtsf)) 
      {   
         if ($_FILES["flyer"]["error"] > 0) 
         {
             echo "Return Code: " . $_FILES["flyer"]["error"] . "<br>";
         }   
             else 
                 {
                if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
                     {
                        unlink("../flyers/" . $_FILES["flyer"]["name"]);
                     }
      move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);             
      $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];
                 }
      }
rhill45
  • 559
  • 10
  • 35
  • Use `empty()` and `isset()` with a conditional statement. `if(condition){ do something } else { do something else }` – Funk Forty Niner Jan 17 '15 at 18:43
  • @Fred-ii- can you post example. I know this is pretty basic :( my html is – rhill45 Jan 17 '15 at 18:45
  • I don't see how that could happen, the first `if` statement would evaluate to `false` for an empty field. – jeroen Jan 17 '15 at 18:46
  • @jeroen I'm using ajax if that matters, so the submit button is really not directing the form – rhill45 Jan 17 '15 at 18:47
  • 1
    This Q&A http://stackoverflow.com/q/2958167/ will better illustrate, and http://stackoverflow.com/a/10198466/ - As per first results found in Googling "check if no file uploaded php". ;-) – Funk Forty Niner Jan 17 '15 at 18:47

0 Answers0