From 3731f5054a6809f8c9be48290335af3f959a5bbf Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 26 May 2017 12:47:54 -0400 Subject: [PATCH] Setup file size max for the upload. Also setting up javascript which will check the file size to make sure it no larger than 1MB. File is uploaded to uploads/glm-support and remove from the server when the file get's sent in the email as an attachment. --- controllers/admin.php | 24 ++++++++++++++++++------ views/supportForm.php | 25 +++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/controllers/admin.php b/controllers/admin.php index 0cd3cc0..27a894c 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -123,13 +123,19 @@ class SupportAdmin $attachment = ''; if ( isset( $_FILES ) && is_array( $_FILES ) && isset( $_FILES['file_upload'] ) && !$_FILES['file_upload']['error'] ) { + // Create a directory in uploads for support files if needed + $upPath = wp_upload_dir(); + $filePath = $upPath['basedir'] . '/glm-support'; + if ( !is_dir( $filePath ) ) { + $oldumask = umask( 0 ); + mkdir( $filePath, 0770 ); + umask( $oldumask ); + } // move the uploaded file - $upload_path = wp_upload_dir(); - $file_name = $upload_path['basedir'] . '/' . $_FILES['file_upload']['name']; - $fileUploaded = move_uploaded_file( $_FILES['file_upload']['tmp_name'], $file_name ); - echo '
$_FILES: ' . print_r( $_FILES, true ) . '
'; - if ( $fileUploaded ) { - $attachment = $fileUploaded; + $fileName = wp_unique_filename( $filePath, $_FILES['file_upload']['name'] ); + $fileUploaded = move_uploaded_file( $_FILES['file_upload']['tmp_name'], $fileName ); + if ( $fileUploaded && $fileName ) { + $attachment = $fileName; } } @@ -140,6 +146,12 @@ class SupportAdmin $hdrs, $attachment ); + + + // If there's a file attachment then delete file from server + if ( isset( $fileName ) && $fileName && is_file( $filePath . '/' . $fileName ) ) { + unlink( $filePath . '/' . $fileName ); + } } } } diff --git a/views/supportForm.php b/views/supportForm.php index 5017e35..a3c22ad 100644 --- a/views/supportForm.php +++ b/views/supportForm.php @@ -72,9 +72,11 @@

Use this support form to quickly report an issue with your web site.

Please provide as much information as you can.

+

If you have a screenshot to upload the file must be no larger than 1MB.

+ @@ -129,10 +131,11 @@ @@ -144,6 +147,24 @@
Please help us provide the proper support
- * Upload a screenshot: (jpg,gif,png) + * Upload a screenshot: + (1MB Max) - +

* denotes required field

+
+ -- 2.17.1