Unable to upload specific .zip file type via frontend in wordpress 4.0
-
I allow to upload a selected few type of files from the frontend eg. .zip, .mp4, .jpeg and .pdf. Till now all these file types had no issues getting attached to the post it is uploaded to. However after upgrading to wordpress 4.0, I have not been able to upload specific .zip file type from the frontend, although it uploads well from the backend using media-upload interface. Please note that the other file types .mp4 and .pdf files still attach with no issues.
Given below is an entire code that performs the attachment function.
if ( isset( $_POST['upload_attachments'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce($_POST['secure_upload'], 'upload_attachments_nonce')) { //checking if upload is empty //checking if filesize is valid if ($_FILES) { //https://wordpress.stackexchange.com/questions/39753/ //loop through multiple files. $files = $_FILES['upload']; foreach ($files['name'] as $key => $value) { if ($files['name'][$key]) { $file = array( 'name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key] ); $uploaded_file_type = $files['type'][$key]; $image_file_types = array('image/jpg', 'image/jpeg', 'image/png'); $appnvid_file_types = array('application/pdf', 'application/zip', 'video/mp4'); $uploaded_file_size = $files['size'][$key]; $size_in_kb = $uploaded_file_size / 1024; $image_size_limit = 1000; // Your Filesize in KB $appnvid_size_limit = 25000; if(in_array($uploaded_file_type, $image_file_types)) { if( $size_in_kb > $image_size_limit ) { $upload_error .= 'Image files must be smaller than '.$image_size_limit.'KB'; return; } else { $_FILES = array("upload" => $file); foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file,$post_id); //return; this loop neds to run multiple times so no return here } } } elseif(in_array($uploaded_file_type, $appnvid_file_types)){ if( $size_in_kb > $appnvid_size_limit ) { $upload_error .= 'File must be smaller than '.$appnvid_size_limit.'KB'; return; }else { $_FILES = array("upload" => $file); foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file,$post_id); header ('Location: ' . $_SERVER['REQUEST_URI']);//Post, redirect and get exit(); //return;//run this loop only once for application and video file type. } } } else { $upload_error .= 'Invalid File type'; } } } } header ('Location: ' . $_SERVER['REQUEST_URI']);//Post, redirect and get exit(); }//end of nonce check
Here is the code for upload helper insert_attachment function
function insert_attachment($file_handler,$post_id,$setthumb='false') { // check to make sure its a successful upload if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false(); require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); $attach_id = media_handle_upload( $file_handler, $post_id ); //if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id); return $attach_id; }
It is not like, the files is not getting attached otherwise I would find it in media-library, the new version seem to completely ignore this specific file type when uploaded from the frontend. This line
$appnvid_file_types = array('application/pdf', 'application/zip', 'video/mp4');
defines all three file types and this lineelseif(in_array($uploaded_file_type, $appnvid_file_types)){
checks the inserted files. I am confused as to why the .mp4 and .pdf gets attached fine and .zip does not ? Plus .zip had been uploading fine till yesterday.I am clueless to what is happening. A little help will be appreciated please.
- The topic ‘Unable to upload specific .zip file type via frontend in wordpress 4.0’ is closed to new replies.