如何检索上传的压缩文件的url

时间:2019-11-30 作者:DaF-Student

下面的PHP代码允许用户在WordPress前端上传文件。我搜索了几个小时,但无法显示上传文件的URL。

我输入索引。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 ?>
这就是函数。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;
}
附加问题此代码不允许上载rar文件它只上载zip文件,是否也可以上载rar文件?

提前感谢

1 个回复
SO网友:Charles

要允许上载不在MIME“常规”列表中的文件,您必须告诉WordPress将其添加到该列表中。

作为example 下面的代码将这些扩展添加到已经存在的MIME列表中。

请先备份

/**
 * Source: @link https://developer.wordpress.org/reference/hooks/upload_mimes/
 *
 */
add_filter( \'upload_mimes\', \'add_custom_upload_mimes\' );
function add_custom_upload_mimes( $existing_mimes )
{
   return array_merge( $existing_mimes, array(
      \'rar\' => \'application/rar\'
    ));

  return $existing_mimes;
}
它首先获取允许的文件类型(existing\\u mimes)的现有数组,并向其中添加“new”
“新”数组将返回到WordPress。

我希望这将有助于你的方式,当它是关于允许\'新\'的文件。要上载的ext。

edit:简化的代码,可能还有给定的MIME类型。rar扩展(将其更改为应用程序,请参阅代码)错误?!

相关推荐

使用unctions.php中的代码更改form-check out.php

具体来说,如何删除“您的订单”或完整功能?通过在函数中使用代码段。php(子主题)可通过以下方式找到:<h3 id=\"order_review_heading\"><?php esc_html_e( \'Your order\', \'woocommerce\' ); ?></h3> 表单签出。php。我更喜欢一些代码,而不是将文件处理为子主题并更改文件。