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

时间:2018-02-01 21:58:33

标签: php ruby-on-rails ruby wordpress

我有这个脚本在ruby on rails上运行,这是一个发票下载系统,非常简单。它下载已生成的存储在服务器中的发票。用户只需输入作品集和发票号。现在我需要与wordpress相同的功能。我想我需要用PHP。

这是rails控制器上的原始ruby。

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

这就是我在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 个答案:

没有答案