title: Unable to upload file using PHP
body: <p>I am unable to upload files to my CentOS 7 server running httpd using php.</p>
  
  <p>I have searched all over and still cannot figure out why I am unable to upload files. :(</p>
  
  <p>Here is the form:</p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-lg-4 col-lg-offset-4"&gt;
              &lt;h2 class="section-heading text-white text-center"&gt;Private Upload Page&lt;/h2&gt;
              &lt;hr class="light"&gt;
              &lt;form class="form-horizontal" role="form" action="upload.php" method="post" enctype="multipart/form-data"&gt;
                &lt;div class="form-group"&gt;
                &lt;span class="text-white"&gt;Select directory to upload to:&lt;/span&gt;&lt;br&gt;
                &lt;input class="form-control" type="text" name="dir" value="/var/www/html/Private/uploads" style="width: 100%;"&gt;&lt;br&gt;
                &lt;span class="text-white"&gt;Select file to upload:&lt;/span&gt;&lt;br&gt;
                &lt;input class="form-control" type="file" name="fileToUpload" id="fileToUpload" style="width: 100%;"&gt;&lt;br&gt;
                &lt;input class="form-control btn btn-primary" type="submit" value="Upload File" name="submit" &gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And here is the php file:</p>
  
  <pre><code>&lt;?php
  // Check directory from form, otherwise use uploads folder in 
  // /var/www/html/Private
  if(isset($_POST["dir"])) {
      $target_dir = $_POST["dir"];
      echo "&lt;p&gt;Using " . $target_dir . " for directory.&lt;/p&gt;&lt;br&gt;";
  }
  else {
      $target_dir = "/var/www/html/Private/uploads/";
      echo "&lt;p&gt;Using /var/www/html/Private/uploads for directory.&lt;/p&gt;&lt;br&gt;";
  }
  
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  
  // Check if file already exists
  if (file_exists($target_file)) {
      echo "Sorry, file already exists.";
      $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
      echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  } else {
      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
          echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
      } else {
      echo "Sorry, there was an error uploading your file.";
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I have tried setting using:</p>
  
  <pre><code>chmod 777 /var/www/html
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>chmod 777 /var/www
  </code></pre>
  
  <p>as well as turned on file write in the php.ini file</p>
  
  <p>as well as turning off the firewall and am still unable to upload a file. </p>
  
  <p>I am newish to web design, so I'm sure it is an issue that I caused. Every page I looked at pointed to permissions but even allowing 777 still did not fix my issue.</p>