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 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持