PHP:通过WordPress中的链接点击通过AJAX下载文件

时间:2019-01-05 20:44:00

标签: php ajax wordpress

我当前正在尝试通过PHP AJAX请求从服务器下载文件。我是这样构建的:

首先,我要获取所有文件(在此示例中只有一个)并建立链接:

$invoice_number_base = 'RE-2018-12-00000039-E';

//Get all generated PDF file names by tmp path and invoice number base
foreach ( glob( '/var/www/vhosts/localhost/httpdocs/wp-content/uploads/wpo_wcpdf/attachments/' . $invoice_number_base . '*.pdf' ) as $file ) { ?>
    <a target="_blank" class="admin_et_pb_button"
       onclick="showGenInvoice('<?php echo $file ?>')">
        <?php echo basename( $file ) ?>
    </a>
<?php }

这将在此处生成此链接:

<a target="_blank" class="admin_et_pb_button" onclick="showGenInvoice('/var/www/vhosts/localhost/httpdocs/wp-content/uploads/wpo_wcpdf/attachments/RE-2018-12-00000039-E.pdf')">RE-2018-12-00000039-E.pdf</a>

现在,我已经构建了我的JS函数,以在用户单击按钮时调用AJAX函数:

function showGenInvoice(file) {

    var data = {
        'action': 'show_gen_invoice',
        'file': file
    };

    jQuery.post(ajaxurl, data, function () {
    }).fail(function () {
        alert('An error occured!')
    });
}

(该函数具有参数链接,其中包含服务器上每个文件的路径)

此后,我在WordPress中构建了AJAX回调:

/**
 * Get generated invoice from attachments folder so the invoices which are sent by email
 */
add_action( 'wp_ajax_show_gen_invoice', array( $this, 'show_gen_invoice' ) );
public function show_gen_invoice() {
    //Get file path from request
    $file = $_POST['file'];
    if ( is_admin() && file_exists( $file ) ) {
        header( 'Content-Description: File Transfer' );
        header( 'Content-Type: application/octet-stream' );
        header( 'Content-Disposition: attachment; filename="' . basename( $file ) . '"' );
        header( 'Expires: 0' );
        header( 'Cache-Control: must-revalidate' );
        header( 'Pragma: public' );
        header( 'Content-Length: ' . filesize( $file ) );
        ob_clean();
        flush();
        readfile( $file );
        wp_die();
    } else {
        wp_send_json_error( null, 500 );
        wp_die();
    }
}

但是不幸的是,当我按下按钮时,没有文件被下载。没有错误,什么都没有发生。怎么了?

注意:

  

文件所在的文件夹受保护,无法访问   带有正常的页面网址和/ uploads /...

更新

请签出我的解决方案!当您想使用PHP从后端从服务器下载某些内容时,此功能非常有用。

1 个答案:

答案 0 :(得分:0)

感谢提示。这是我针对该问题的解决方案:

新链接:

//Get invoices tmp path
$tmp_path = WPO_WCPDF()->main->get_tmp_path( 'attachments' );
//Get invoice number
$invoice_number_base = $invoice->get_number()->get_formatted(); ?>
<div class="wpo_wcpdf-generated-invoices-container">
    <?php
    //Get all generated PDF file names by tmp path and invoice number base
    foreach ( glob( $tmp_path . $invoice_number_base . '*.pdf' ) as $invoice ) { ?>
        <a target="_blank"
           class="admin_et_pb_button"
           href="post.php?invoice=<?php echo basename( $invoice ) ?>">
            <?php echo basename( $invoice ) ?>
        </a>
    <?php } ?>
</div>
<?php

在浏览器中显示文件的代码:

/**
 * Add file download functionality to admin post
 */
add_action( 'admin_init', 'show_gen_invoice' );
function show_gen_invoice() {

    //Check if invoice is set and get value = the path of the invoice
    if ( isset( $_REQUEST['invoice'] ) ) {

        //Get invoices tmp path
        $tmp_path = WPO_WCPDF()->main->get_tmp_path( 'attachments' );

        //Get invoice basename
        $invoice = urldecode( $_REQUEST['invoice'] );

        if ( is_admin() && file_exists( $tmp_path . $invoice ) ) {
            $content = file_get_contents( $tmp_path . $invoice );
            header( 'Content-Type: application/pdf' );
            header( 'Content-Length: ' . filesize( $tmp_path . $invoice ) );
            header( 'Content-Disposition: inline; filename=' . $invoice );
            header( 'Cache-Control: private, max-age=0, must-revalidate' );
            header( 'Pragma: public' );
            ini_set( 'zlib.output_compression', '0' );
            flush();
            die( $content );
        }
    }
}

我现在已经在浏览器中显示了该文件,但是如果您想直接下载该文件,则可以使用上面的工作代码进行下载。

我现在正在使用admin_init钩子将代码放入post.php文件中,在这里我可以使用$_REQUEST来获取此文件。