Saving a file uploaded using POST

  //-----------------------------------
  //----- CHECK FOR FILE UPLOADED -----
  //-----------------------------------
  $FileUploadValid = True;      //Default tovalid  file being uploaded

  //Check the files array isn't empty
  if(!empty( $_FILES ))
  {
    //Verify that the file being uploaded is one of our supported types
    if ($_FILES['config_file']['type'] != "image/jpeg")
      $FileUploadValid = False;

    //print_r($_FILES);
  }
  else
  {
    $FileUploadValid = False;
  }

  if ($FileUploadValid)
  {
    //--------------------------
    //----- STORE THE FILE -----
    //--------------------------

    //Get file extension
    $path = $_FILES['my_file_input_name']['name'];
    $UploadedFileName = strtolower(pathinfo($filename, PATHINFO_FILENAME));
    $UploadedFileExtension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if (strlen($UploadedImageFileExtension) > 4)
      $UploadedImageFileExtension = 'err';

    $SaveAsFilename = "uploadedfile.txt";

    //----- WRITE THE FILE -----
    $OurFileUploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . '/uploaded_files';

    //Ensure directory exists
    if (!file_exists($OurFileUploadsDirectory))
    {
      admin_event_db_add_event(0, "creating file uploads directory");

      wp_mkdir_p($OurFileUploadsDirectory);
      chmod($OurFileUploadsDirectory, 0755);     //Set folder permissions to allow read of file added here
    }

    //Write the file
    $Source = $_FILES['my_file_input_name']['tmp_name'];
    $OurFileUploadsDirectory = trailingslashit($OurFileUploadsDirectory);
    move_uploaded_file($Source, $OurFileUploadsDirectory . $SaveAsFilename);

    if (!is_file($OurFileUploadsDirectory . $SaveAsFilename))
    {
      //----- FILE SAVE FAILED -----
      $FileUploadValid = False;

    }

  } //if ($FileUploadValid)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *