how to retrieve uploaded url of zip files

Question

following PHP code allow users to upload files frontend in wordpress
I’ve searched hours but I could not manage to display URLs of uploaded files.

I put in index.php

    <?php
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 universal filesize is valid

    if ($_FILES) { //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];
                $allowed_file_types = array('application/x-zip-compressed', 'image/jpg', 'image/jpeg', 'image/png', 'application/pdf', 'application/zip', 'video/mp4');
                $uploaded_file_size = $files['size'][$key];
                $size_in_kb = $uploaded_file_size / 1024;
                $file_size_limit = 10000; // Your Filesize in KB


                if(in_array($uploaded_file_type,  $allowed_file_types)) { 
                   if( $size_in_kb > $file_size_limit ) {
                   $upload_error .= 'Image files must be smaller than '.$file_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
                 }      
                 }
                 } else { $upload_error .= 'Invalid File type';
                         return;
                        }                                                              

                }
            }
        }                  

    header ('Location: ' . $_SERVER['REQUEST_URI']);//Post, redirect and get
    exit();
    }//end of nonce check ?>

and this is functions.php

    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;
}

additional question this code wont allow to upload rar file it uploads zip files only, is it possible to upload with it rar files too?

Thanks in advance

0
, DaF-Student 4 years 2019-11-30T10:13:33-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse