Upload and Rename a File

I have a couple of applications where users need to upload files regularly, and have them renamed and placed into a specific directory. (These applications run independently from WP, so this is just straight PHP.

The form:

<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type=hidden name="filename" value="renamed.pdf">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

 The processing page:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

      if (file_exists("scholarships/" . $_FILES["file"]["name"]))
      {
          unlink ("scholarships/" . $_FILES["file"]["name"]);
      }
      move_uploaded_file($_FILES["file"]["tmp_name"],"scholarships/" . $_POST["filename"]);
      echo "File uploaded and renamed" ;

    
     } else {
     echo "File not uploaded";
}
?>

 

Comments are closed.

Skip to toolbar