RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 916211
Accepted
zeni1agent
zeni1agent
Asked:2020-12-06 15:50:18 +0000 UTC2020-12-06 15:50:18 +0000 UTC 2020-12-06 15:50:18 +0000 UTC

如何从元字段下载文件并统计下载次数?

  • 772

我有这个包含附件 id 的元字段

例如

get_post_meta(get_the_ID(), 'test_file', true)

我需要下载此文件并使用此代码计算元字段中的下载次数。

$postID = get_the_ID;
$number_downlode = get_post_meta($postID, 'downlode_registration', true);
$number_downlode++;
update_post_meta($postID, 'downlode_registration', $number_downlode);

但我不知道该怎么做。

我尝试使用 onclick 命令和 ajax

<?php setPostViews(get_the_ID()); ?>    
<?php $GET11 = wp_get_attachment_url ( get_post_meta(get_the_ID(), 'test_file', true) );?>  
<a href="<?php echo $GET11 ?>" download onclick="isEmail()">Click Me</a>
<script type="text/javascript">
function isEmail() {
    $(document).ready(function(){
            $.ajax({
                type: 'POST',
                url: 'script.php',
                success: function(data) {
                 document.write("data");
                }
            });

}); }
</script>

脚本.php

$postID = get_the_ID;
$number_downlode = get_post_meta($postID, 'downlode_registration', true);
$number_downlode++;
update_post_meta($postID, 'downlode_registration', $number_downlode);

但是script.php文件看不到wordpress

我还尝试使用 POST 方法下载文件

<?php 
  if ( get_post_meta(get_the_ID(), 'test_file', true) ) : 
  $GET11 =   get_post_meta(get_the_ID(), 'test_file', true) ;
?>
<form method="POST">
    <input type="submit" value="downlode" name="test_downlode">
</form>
<?php if ($_POST['test_downlode']){ 
$number_downlode = get_post_meta($postID, 'downlode_registration', true);
$number_downlode++;  
update_post_meta($postID, 'downlode_registration', $number_downlode);
$file = ($GET11);
 if (file_exists($file)) {
    if (ob_get_level()) {
      ob_end_clean();}
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;}}
  endif; ?>

但是无论我尝试了多少,页面都看不到 $file 变量中的文件

我需要通过元字段完成计算,因为稍后我将通过 get_posts() 过滤页面。

插件在这里不起作用。

wordpress
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    KAGG Design
    2020-12-09T00:26:08Z2020-12-09T00:26:08Z

    要解决这个问题,你需要做两件事:拦截点击链接并将文件提供给使用 php.ini 的真实用户。

    当您单击指向不存在的页面的虚拟链接时会发生什么?WordPress 初始化内核并尝试发出 404 页面,此时您需要干预您的函数,更新下载计数器并给出真实文件。以下是此类功能的代码。

    /**
     * Rewrite download link by its url.
     * Increase download count.
     */
    public function rewrite_download_link() {
        /** @var wpdb $wpdb */
        global $wpdb;
    
        $uri  = $_SERVER['REQUEST_URI'];
        $path = wp_parse_url( $uri, PHP_URL_PATH );
    
        if ( 0 === strpos( trailingslashit( $path ), self::$link_base ) ) {
            $download_link = untrailingslashit( $path );
            $url           = self::get_url( $download_link );
            if ( $url ) {
                $count = self::get_count( $url );
                $data  = array(
                    'count' => ++ $count,
                );
                $where = array(
                    'url' => $url,
                );
                $wpdb->update( self::$table, $data, $where );
    
                self::download_file( $url );
            }
        }
    }
    

    该函数rewrite_download_link()在 event 上触发init,并检查 url 是否以 开头$link_base,例如,它可能是/downloads/. 任何视图链接都被http://site.org/downloads/cool.jpg认为是虚拟的,需要进行处理,更新下载计数器,计算文件的真实 url,并调用函数将真实文件返回给用户。

    该函数的代码如下所示。

    /**
     * Download file.
     *
     * @param string $url file url.
     */
    private static function download_file( $url ) {
        if ( ! $url ) {
            return;
        }
    
        $file_path = ABSPATH . wp_make_link_relative( $url );
    
        $file_name = pathinfo( $file_path, PATHINFO_FILENAME );
    
        $file_extension = pathinfo( $file_path, PATHINFO_EXTENSION );
    
        switch ( $file_extension ) {
            case 'png':
                $content_type = 'image/png';
                break;
            case 'gif':
                $content_type = 'image/gif';
                break;
            case 'tiff':
                $content_type = 'image/tiff';
                break;
            case 'jpeg':
            case 'jpg':
                $content_type = 'image/jpg';
                break;
            default:
                $content_type = 'application/force-download';
        }
    
        header( 'Expires: 0' );
        header( 'Cache-Control: no-cache, no-store, must-revalidate' );
        header( 'Cache-Control: pre-check=0, post-check=0, max-age=0', false );
        header( 'Pragma: no-cache' );
        header( "Content-type: {$content_type}" );
        header( "Content-Disposition:attachment; filename={$file_name}.{$file_extension}" );
        header( 'Content-Type: application/force-download' );
    
        // @codingStandardsIgnoreLine
        readfile( "{$file_path}" );
        exit();
    }
    

    该函数download_file()使接收到的文件相对链接并计算文件的路径,然后使用 php.ini 将其返回。因此,用户无法找到真实文件的位置并直接下载。

    包含上述方法的完整类代码可以在我们的 GitHub 上找到。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5