0

I have written a custom php that allows a user from my templated page from the front end to upload files in a newly created subfolder which takes user_login as its name within the uploads folder. So the root folder is uploads and inside the user's subfolder are stored all the files. This is my code.

<form enctype="multipart/form-data" action="" method="POST" class="centered">   

<p></p>

<p>
Select a document to upload
</p>

<?php 


$user = wp_get_current_user();

$dirname = $user->user_login;

$upload_dir   = wp_upload_dir();
// creates a user subdir for the files
if ( isset( $current_user->user_login ) && ! empty( $upload_dir['basedir'] ) ) {
    $user_dirname = $upload_dir['basedir'].'/'.$current_user->user_login;
        if ( ! file_exists( $user_dirname ) ) {
        wp_mkdir_p( $user_dirname );
    }
}


if(isset($_FILES['document'])){

$errors = array();

$document = $_FILES['document']['name'];
$file_tmp = $_FILES['document']['tmp_name'];

if(empty($errors)==true){
         move_uploaded_file($file_tmp,"/$user_dirname/".$document);
         echo "The file was uploaded successfully!";
      }else{
         print_r($errors);
      }

}

?>


<input type="file" name="document" class="centered" value="">
<p></p>
<input type="submit" value="Upload Document" name="submit">

</form>

As you can see I haven't used any of the WP functionality, because I am not very experienced, but I would like to create an action (I believe this is the right word) that sends email notifications every time a file is uploaded to the uploads folder. What hook do I need to use and/or in what way do I need to modify my code to be able to user the wordpress actions?

I have found this action publish_post that provides an example on how to send an e-mail when a post is created. However my code doesn't create a post and it wouldn't work in a case where I have a batch program doing the uploads. Is there an action that triggers when a file is uploaded that I could use?

My background is Laravel, so I'm still learning the WP ways of doing things and using mainly PHP with limited use of WP functionalities.

1

You could use the wp_mail function https://developer.wordpress.org/reference/functions/wp_mail/ to send an email to anyone after the file is successfully uploaded in your directory. You do not need any action here

Add it your code after the file is uploaded successfully

if(empty($errors)==true){
         move_uploaded_file($file_tmp,"/$user_dirname/".$document);
         echo "The file was uploaded successfully!";
         wp_mail(..);
      }else{
         print_r($errors);
      }

}
  • Could I do that by inserting it inside my code that uploads the file? – R1ddler Oct 17 '17 at 12:46
  • Do I need to configure SMTP? Because I am certain the code I have written is functional, but I am not receiving any e-mails. – R1ddler Oct 17 '17 at 14:44
  • Found an answer here flippercode.com/send-html-emails-using-wp-mail-wordpress looks like you will have to add phpmailer_init action – Sneha Agarwal Oct 17 '17 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.