title: PHP - how to do file upload and error handling?
body: <p>I'm new to php and the below user signup form is giving me some real trouble.
  All inputs of the form are being registered correctly, except the file input type and I can't figure out why. I've stripped my code down to try narrow down the issue but I can't pinpoint it.</p>
  <p>I want php to check if a user uploaded their own image if they selected that option within the form below:</p>
  <p>The form on the homepage:</p>
  <pre><code>&lt;form class=&quot;signupform&quot; action=&quot;backgr/signup-inc.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  
  
        &lt;p&gt;
        &lt;label&gt; Name: &lt;/label&gt;
        &lt;input type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;Jane Doe&quot; required&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label&gt; Email: &lt;/label&gt;
        &lt;input type=&quot;Email&quot; name=&quot;email&quot; placeholder=&quot;janedoe@hotmail.com&quot; required&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label&gt;Date of birth: &lt;/label&gt; 
        &lt;input type=&quot;date&quot; name=&quot;dob&quot; required&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label&gt;Username: &lt;/label&gt; 
        &lt;input type=&quot;text&quot; name=&quot;uid&quot; placeholder=&quot;MyUsername&quot; required&gt;
        &lt;/p&gt;
  
        &lt;p&gt; 
        &lt;label&gt; Password:&lt;/label&gt;
        &lt;input type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;passwordYD&quot; required&gt;
        &lt;/p&gt;
  
        &lt;p&gt; 
        &lt;label&gt; Confirm Password:&lt;/label&gt;
        &lt;input type=&quot;password&quot; name=&quot;pwdconfirm&quot; placeholder=&quot;passwordYD&quot; required&gt; 
        &lt;/p&gt;
  
        &lt;p&gt;
        Upload a profile picture or select a picture below!
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;input type=&quot;radio&quot; name=&quot;profilepic&quot; value=&quot;profilepic1&quot; required&gt;&lt;label&gt;&lt;img class=&quot;profilepic&quot; src=&quot;backgr/graphics/profileimgs/penguin.png&quot;&gt;&lt;/label&gt;&lt;br&gt;
        &lt;input type=&quot;radio&quot; name=&quot;profilepic&quot; value=&quot;profilepic2&quot; required&gt;&lt;label&gt;&lt;img class=&quot;profilepic&quot; src=&quot;backgr/graphics/profileimgs/sheep.png&quot;&gt;&lt;/label&gt;&lt;br&gt;
        &lt;input type=&quot;radio&quot; name=&quot;profilepic&quot; value=&quot;profilepic3&quot; required&gt;&lt;label&gt;&lt;img class=&quot;profilepic&quot; src=&quot;backgr/graphics/profileimgs/smiley.png&quot;&gt;&lt;/label&gt;&lt;br&gt;  
  
  
        &lt;input type=&quot;radio&quot; name=&quot;profilepic&quot; value=&quot;profilepic4&quot; required&gt;  &lt;label&gt; Upload your own picture!&lt;/label&gt;
        &lt;input type=&quot;file&quot; name=&quot;profilefile&quot; accept=&quot;.jpg,.png,.jpeg&quot;&gt;
        &lt;/p&gt;&lt;br&gt;
  
  
        &lt;p&gt;
        &lt;label&gt; How did you hear about YaDunya? &lt;/label&gt;
        &lt;select id=&quot;howheard&quot; name=&quot;howheard&quot;&gt; 
            &lt;option value=&quot;Recommend&quot;&gt; Recommended by someone I know&lt;/option&gt;
            &lt;option value=&quot;Social Media&quot;&gt; Social Media (YouTube, Instragram, Facebook, Twitter) &lt;/option&gt;
            &lt;option value=&quot;Internet&quot;&gt; Internet Search (e.g. Google search results) &lt;/option&gt;
        &lt;/select&gt; &lt;br&gt;
        &lt;/p&gt;
  
  
        &lt;p class=&quot;tndc&quot;&gt;
          You mustr read and accept Ya Dunya Terms and Conditions to sign up, please click below to indicate acceptance. 
        &lt;/p&gt;
        &lt;p class=&quot;tandc&quot;&gt; 
        I have read and accept Ya Dunya &lt;a href=&quot;terms.php&quot; target=&quot;_blank&quot;&gt;Terms and Conditions&lt;/a&gt; to continue signing up.
        &lt;input class=&quot;tandc&quot; name=&quot;tandc&quot; type=&quot;checkbox&quot; required&gt;
        &lt;/p&gt;&lt;br&gt;
  
        &lt;button id=&quot;submitsignbtn&quot; name=&quot;signupsubmit&quot; type=&quot;submit&quot;&gt;Submit &lt;/button&gt;
      &lt;/form&gt;
  
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- PHP code to give user messages about their sign up--&gt;
  &lt;?php
      if(isset($_GET[&quot;error&quot;])) {               //check if a certain word is contained in the url, POST is data we can't see in the url, GET is data we can see inside the url
            if ($_GET[&quot;error&quot;] == &quot;emptyinput&quot;) {                    //checking what the error message in the url is
              echo &quot;&lt;script&gt;alert('Please fill in all fields to sign up! ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;invaliduid&quot;) {
              echo &quot;&lt;script&gt;alert('Please choose a username containing only alphabetical or numerical characters. ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;invalidemail&quot;) {
              echo &quot;&lt;script&gt;alert('Please choose a correct email address. ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;pwdsnomatch&quot;) {
              echo &quot;&lt;script&gt;alert('Please ensure both passwords are the same. ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;stmtfailed&quot;) {
              echo &quot;&lt;script&gt;alert('Something went wrong, please try again! ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;uidexists&quot;) {
              echo &quot;&lt;script&gt;alert('Username already exists, please choose another username! ')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;uploadpic&quot;) {
              echo &quot;&lt;script&gt;alert('Please upload a profile picture!')&lt;/script&gt;&quot;;
            }
            else if ($_GET[&quot;error&quot;] == &quot;none&quot;) {
              echo &quot;&lt;script&gt;alert('You have signed up, welcome to Ya Dunya! ')&lt;/script&gt;&quot;;
            }
      }
  ?&gt;
  </code></pre>
  <p>The php page the form is calling (action=&quot;signup-inc.php):</p>
  <pre><code>&lt;?php
  //Don't add php close tags
  
  /* Making sure users can only access this page if they submitted the form the proper way, rather than just typing the url for this page*/
  if (isset($_POST[&quot;signupsubmit&quot;])) { //if this is inside the form code then proceed if not don't, based on them clicking the submit button named &quot;submit&quot;
      
  
  /* Create unique variables for each data element passed by the user in the form */
      $name=$_POST[&quot;name&quot;];
      $email=$_POST[&quot;email&quot;];
      $dob=$_POST[&quot;dob&quot;];
      $username=$_POST[&quot;uid&quot;];
      $pwd=$_POST[&quot;password&quot;];
      $pwdconfirm=$_POST[&quot;pwdconfirm&quot;];
      $howheard=$_POST[&quot;howheard&quot;];
      $tandc=$_POST[&quot;tandc&quot;];
      $profilepic=$_POST[&quot;profilepic&quot;];
  
  $target_dir = &quot;profiles/&quot;;
  $target_file = $target_dir . basename($_FILES[&quot;profilefile&quot;][&quot;name&quot;]);
  $uploadOk = 1;
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  
  
  /* Create error handling in case the user has made mistakes by linking to error doc and connect to db doc */
  require_once 'dbconn-inc.php';
  require_once 'functions-inc.php';
  
  
  
  
  /* using the functions for error handling*/
          if (emptyInputSignup($name,$email,$dob,$username,$pwd,$pwdconfirm,$howheard,$tandc) !==false) { //must say not equal to false because it may return true even though there is an error, the function is checking for empty fields in the form, big difference between not false and true in this case
  
              header (&quot;location:../index.php?error=emptyinput&quot;); //returns the user to the page and includes an error message by including this in the url so that it can be grabbed for an error message script
              exit (); //stops the script running once done
          }
  
          //checking the username
          if (invalidUid($username) !==false) {
          header (&quot;location:../index.php?error=invaliduid&quot;); 
              exit (); 
          }
  
          //checking the email
          if (invalidEmail($email) !==false) {
          header (&quot;location:../index.php?error=invalidemail&quot;); 
              exit (); 
          }
  
          //making sure both passwords match with a function we create
          if (pwdMatch($pwd, $pwdconfirm) !==false) {
          header (&quot;location:../index.php?error=pwdsnomatch&quot;); 
              exit (); 
          }
  
          //checking that the username and email doesn't exist already
          if (uidExists($conn, $username, $email) !==false) {     //need to connect to the db to check this
          header (&quot;location:../index.php?error=uidexists&quot;); 
              exit (); 
          }
  
          //PROFILE PIC UPLOAD IF USER SELECTED OPTION TO UPLOAD PROFILE PIC
          if ($profilepic == 'profilepic4') { 
          
              // Check if image file is a actual image or fake image
              $check = getimagesize($_FILES[&quot;profilefile&quot;][&quot;tmp_name&quot;]);
              if($check !== false) {
              echo &quot;File is an image - &quot; . $check[&quot;mime&quot;] . &quot;.&quot;;
              $uploadOk = 1;
              } else {
              echo &quot;File is not an image.&quot;;
              $uploadOk = 0;
              }
              
  
              // Check if file already exists
              if (file_exists($target_file)) {
                echo &quot;Sorry, file already exists.&quot;;
                $uploadOk = 0;
              }
  
              // Check file size
              if ($_FILES[&quot;profilefile&quot;][&quot;size&quot;] &gt; 500000) {
                echo &quot;Sorry, your file is too large.&quot;;
                $uploadOk = 0;
              }
  
              // Allow certain file formats
              if($imageFileType != &quot;jpg&quot; &amp;&amp; $imageFileType != &quot;png&quot; &amp;&amp; $imageFileType != &quot;jpeg&quot;
              &amp;&amp; $imageFileType != &quot;gif&quot; ) {
                echo &quot;Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.&quot;;
                $uploadOk = 0;
              }
  
              // Check if $uploadOk is set to 0 by an error
              if ($uploadOk == 0) {
                echo &quot;Sorry, your file was not uploaded.&quot;;
              // if everything is ok, try to upload file
              } else {
                if (move_uploaded_file($_FILES[&quot;profilefile&quot;][&quot;tmp_name&quot;], $target_file)) {
                  echo &quot;The file &quot;. htmlspecialchars( basename( $_FILES[&quot;profilefile&quot;][&quot;name&quot;])). &quot; has been uploaded.&quot;;
                } else {
                  echo &quot;Sorry, there was an error uploading your file.&quot;;
                }
          }
  
  }
  
  /* If no errors, create the user in the database */
  createUser($conn,$name,$email,$dob,$username,$pwd,$howheard,$tandc);
  
  }
  else {
      header (&quot;location:../index.php&quot;); //the .. goes outside the includes folder
      exit (); //stops the script running once done
  }
  </code></pre>
  <p>It all works fine until I try to add the part  &quot;//PROFILE PIC UPLOAD ...&quot; into the signup-inc.php page. Once I add that in, it should upload the image or else give error messages - it does neither of those things, instead it just signs the user up as long as the other inputs were completed correctly byt the user.</p>
  <p>I'm doing this on a local XAMPP server so I'm not sure if something along those lines is affecting the issue (file upload via http is ON within the Apache server php.ini file).</p>
  <p>Or, I was wondering was it because I wrote the directory where the file should save incorrectly?   If I want users' pictures to save in a folder outside of the folder that signup-inc.php page is in, do I write the folder destination for the file like this: &quot;../profiles/&quot; or like this: &quot;/profiles/&quot; ?</p>