Handling a file passed as a POST request

    //-----------------------------------
    //----- CHECK FOR FILE UPLOADED -----
    //-----------------------------------
    $FileUploadValid = True;      //Default to valid  file being uploaded
    
    if (!isset($_FILES['UploadedFile']))      //<<<< 'UploadedFile' is the name of our file field in the post data
      $FileUploadValid = False;
    
    //Check the files array isn't empty
    if(!empty( $_FILES ))
    {
      //Verify that the file being uploaded is one of our supported types
      //if (!is_valid_image_file_type($_FILES['UploadedFile']['type']))
      //  $FileUploadValid = False;
    }
    else
    {
      $FileUploadValid = False;
    }
    
    
    if ($FileUploadValid)
    {
      //--------------------------
      //----- STORE THE FILE -----
      //--------------------------
            
      //Get the file extension
      $UploadedFileName = $_FILES['UploadedFile']['name'];    //<<<< 'UploadedFile' is the name of our file field in the post data
      $UploadedFileNamePreExtension = strtolower(pathinfo($UploadedFileName, PATHINFO_FILENAME));
      $UploadedFileExtension = strtolower(pathinfo($UploadedFileName, PATHINFO_EXTENSION));
      if (strlen($UploadedFileExtension) > 4)
        $UploadedFileExtension = 'err';
      
      $FileId = 0;
      if (is_file($_FILES['UploadedFile']['tmp_name']))   //<<<< 'UploadedFile' is the name of our file field in the post data. 'tmp_name' is the name the server has temporarily given the file
      {
        //----- ADD TO OUR DB -----
        $FileContent = file_get_contents($_FILES['UploadedFile']['tmp_name']);
        $FileId = db_files_add_new($DeviceId, $UploadedFileName, 1, $FileContent);

        
        //----- WRITE TO FILE -----
        $SaveAsFilename = "uploadedfile.txt";             //<<<<<<<<Set this
        $OurFileUploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . '/uploaded_files';

        //Ensure directory exists
        if (!file_exists($OurFileUploadsDirectory))
        {
          wp_mkdir_p($OurFileUploadsDirectory);
          chmod($OurFileUploadsDirectory, 0755);     //Set folder permissions to allow read of file added here
        }

        //Write the file
        $Source = $_FILES['UploadedFile']['tmp_name'];      //<<Change 'UploadedFile' to whatever name is used for the File field
        $OurFileUploadsDirectory = trailingslashit($OurFileUploadsDirectory);
        move_uploaded_file($Source, $OurFileUploadsDirectory . $SaveAsFilename);

        if (!is_file($OurFileUploadsDirectory . $SaveAsFilename))
        {
          //----- FILE SAVE FAILED -----

        }        
        
      } //if (is_file($_FILES['UploadedFile']['tmp_name']))

    } //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 *