douhang8991 2018-02-01 21:58
浏览 113

如何从包含用户提供的信息的表单下载文件

I have this script running on ruby on rails, It's an invoice download system, really simple. It downloads already generated invoices stored in a server. The user just inputs a folio and an invoice number. Now I need the same functionality for wordpress. I think I need it in PHP.

This is the original ruby on rails controller.

require 'open-uri'

class FacturaController < ApplicationController

  def descargar
    id, rfc = params[:id], ''
    rfc = 'ARB821217457' if id == '1'
    rfc = 'MAG821209QF7' if id == '2'
    rfc = 'CTS8906284D2' if id == '3'
    folio, serie, type = params[:folio], params[:serie], params[:type]
    file = open('http://200.52.187.238/xsamanager/downloadCfdWebView?serie=' + serie + '&folio=' + folio + '&tipo=' + type + '&rfc=' + rfc + '&key=80cffa3f17b3bfbc4909aa43c1621583&uuid=')
    if file.content_type == 'application/pdf' or file.content_type == 'application/xml'
      send_data file.read, :filename => serie + '_' + folio + '.' + type
    else
      #flash[:errores] = { :error_factura => 'Ocurrió un error al descargar la factura. Asegurese de que los datos son correctos.' }
      #redirect_to :back
      render :action => 'error'
    end
  end

end

And this is what I have on PHP

<?php
if($_POST)
{

//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die();
} 

$rfc    = "ARB821217457"; //RFC del emisor de las facturas
$key    = '80cffa3f17b3bfbc4909aa43c1621583&uuid='; //Clave para descargar del webserver

//check $_POST vars are set, exit if any missing
if(!isset($_POST["serie"]) || !isset($_POST["folio"]) || !isset($_POST["tipo"]))
{
    die();
}


//Asignacion de variables compatible con PHP4.
$serie       = $_POST["serie"];
$folio       = $_POST["folio"];
$tipo        = $_POST["tipo"];




//proceed with file Download.

if(isset($_REQUEST["factura.pdf"])){
// Get parameters
$file = urldecode($_REQUEST["factura.pdf"]); // Decode URL-encoded string
$filepath = "http://holaenroque.com/test/" . $file;

// Process download
if(file_exists($filepath)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filepath));
    flush(); // Flush system output buffer
    readfile($filepath);
    exit();
    echo 'Hola, SU ARCHIVO SE HA DESCARGADO! ';
}else{
    echo 'ERROR AL DESCARGAR! ';
}
}


}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器