Custom Upload Form Submission Issues
-
Hello,
I have built a custom upload form using the below code. After someone uploads a file, it should go to their respective directory. For example: /wp-content/cloud/USER_NAME/. The problem I’m having is after the form is submitted, the file is not uploaded.
Code in my custom plugin file.
/*File upload form*/ function tnl_cloud_upload_form() { if(is_user_logged_in() ) : tnl_cloud_uploads_save(); ?> <button class="cloud-upload-form-toggle open" aria-expanded="false">Upload File</button> <div class="cloud-upload-form-wrapper" style="display:none;"> <form action="<?php echo site_url('/cloud/'); ?>" method="post" enctype="multipart/form-data> <?php echo wp_nonce_field('cloud-upload-nonce', 'cloud-upload-nonce'); ?> <label for="cloud-upload-label">Upload File</label> <br /> <input type="file" name="cloud-upload" id="cloud-upload-label"/> <input type="submit" value="Upload" /> </form> </div> <?php endif; } add_action('tnl_cloud_upload_form_hook', 'tnl_cloud_upload_form'); /*Save the data*/ function tnl_cloud_uploads_save() { if(!isset($_POST['cloud-upload-nonce']) || !wp_verify_nonce($_POST['cloud-upload-nonce'], 'cloud-upload-nonce') ) { return; } if(!current_user_can('cloud_upload_files') ) { return; } if(!isset($_FILES['cloud-upload']) || $_FILES['cloud-upload']['error'] > 0) { return 'ERROR: something went wrong. Please try again later.'; } $current_user = wp_get_current_user(); $cloud_uploads_user_dir = WP_CONTENT_DIR . '/cloud/' . $current_user->user_login . '/'; $filename = $cloud_uploads_user_dir . basename($_FILES['cloud-upload']['name']); $upload_ok = 1; $filetype = pathinfo($filename,PATHINFO_EXTENSION); if(file_exists($filename) ) { echo 'This file already exists.'; $upload_ok = 0; } else { $upload_ok = 1; } if($_FILES['cloud-upload']['size'] > 500000) { echo 'This file is to large.'; $upload_ok = 0; } else { $upload_ok = 1; } if($filetype != 'jpg' && $filetype != 'png' && $filetype != 'jpeg' && $filetype != 'gif') { echo 'Invalid image format.'; $upload_ok = 0; } else { $upload_ok = 1; } if($upload_ok == 0) { echo 'Please fix above errors and try again.'; } else { if(move_uploaded_file($_FILES['cloud-upload']['tmp_name'], $filename) ) { echo 'Error uploading the file. Please try again later.'; } else { echo 'Upload successful.'; } } }
Source: https://www.w3schools.com/php/php_file_upload.asp
I can see this in my server logs.
::1 - - [01/Jul/2017:16:10:33 -0400] "OPTIONS * HTTP/1.0" 200 126 "-" "Apache/2.4.18 (Ubuntu) (internal dummy connection)" MY_IP - SERVER_NAME [01/Jul/2017:16:11:01 -0400] "POST /cloud/ HTTP/1.1" 200 4556 "https://SITE_ADDRESS/cloud/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"
According to the logs the form is posting but it seems like the upload code is not executing. All directories for this implementation are writable by php. There is nothing produced in the debug_log from WP_DEBUG.
I decided not to use WP core functions for this since I was not sure if I could store the media outside of /wp-content/uploads/. I needed to store it in a specific location for later plans.
Any assistance or suggestions is appreciated.
Thanks.
- The topic ‘Custom Upload Form Submission Issues’ is closed to new replies.