0

我有一个创建 Pass 实例的 getPass.php。这是代码:

//create new pass instance
$coupon = new Pass("pass/source");

//fill in dynamic data
$coupon->content['serialNumber'] = (string)uniqid();
$coupon->content['coupon']['secondaryFields'][0]['value'] = 
    (string)$_POST['name'];
$coupon->content['locations'][0] = 
   $locations[(int)$_POST['location']];

$coupon->writePassJSONFile();

$coupon->writeRecursiveManifest();
$coupon->writeSignatureWithKeysPathAndPassword("pass", '12345');
$fileName = $coupon->writePassBundle();

echo "File saved to - $fileName";

**/* THIS IS WHERE IM TRYING TO PRESENT IT */**
$this->outputPassBundleAsWebDownload($fileName);

然后 Pass.php 做了很多工作,因为我用它来通过电子邮件发送工作通行证,但我用演示/下载功能替换了电子邮件功能。所以这里是 Pass.php 的代码:

<?php
class Pass {
    private $workFolder = null;
    private $ID = null;

    var $content = null;
    var $passBundleFile = null;

    private function copySourceFolderFilesToWorkFolder($path) {
      //recurse over contents and copy files
      $files = new RecursiveIteratorIterator(
                     new RecursiveDirectoryIterator($path), 
                     RecursiveIteratorIterator::SELF_FIRST);

      foreach($files as $name => $fileObject){
        if (is_file($name) && 
            substr($fileObject->getFileName(), 0, 1)!=".") {

          copy($name, 
            $this->workFolder."/".str_replace($path."/", "",$name));
        } else if (is_dir($name)) {
          mkdir($this->workFolder."/".
                str_replace($path."/", "",$name));
        }
      }
    }

    //import a json file into the object
    function readPassFromJSONFile($filePath) {
      //read the json file and decode to an object
      $this->content = 
         json_decode(file_get_contents($filePath),true);
    }

    //export a json file from the object
    function writePassJSONFile() {
      file_put_contents($this->workFolder."/pass.json",
                        json_encode($this->content));
    }

    //generate the manifest file
    function writeRecursiveManifest() {
      //create empty manifest
      $manifest = new ArrayObject();

      //recurse over contents and build the manifest
      $files = new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($this->workFolder), 
                 RecursiveIteratorIterator::SELF_FIRST);

      foreach($files as $name => $fileObject){
        if (is_file($name) && 
              substr($fileObject->getFileName(), 0, 1)!=".") {

          $relativeName = str_replace($this->workFolder.
                                        "/","",$name);

          $sha1 = sha1(file_get_contents(
                         $fileObject->getRealPath()
                  ));
          $manifest[$relativeName] = $sha1;
        }
      }

       //printf debug
       // print_r($manifest);

          //write the manifest file
      file_put_contents($this->workFolder."/manifest.json",
                          json_encode($manifest));
    }

    //generate the bundle signature
    function writeSignatureWithKeysPathAndPassword($keyPath, $pass) {
      $keyPath = realpath($keyPath);


          if (!file_exists($keyPath.'/WWDR.pem')) 
            die("Save the WWDR certificate as
                 $keyPath/WWDR.pem");

      if (!file_exists($keyPath.'/passcertificate.pem')) 
        die("Save the pass certificate as 
             $keyPath/passcertificate.pem");

      if (!file_exists($keyPath.'/passkey.pem')) 
        die("Save the pass certificate key as 
             $keyPath/passkey.pem");

      $output = shell_exec("openssl smime -binary -sign". " -certfile '".$keyPath."/WWDR.pem'".
             " -signer '".$keyPath."/passcertificate.pem'".
             " -inkey '".$keyPath."/passkey.pem'".
             " -in '".$this->workFolder."/manifest.json'".
             " -out '".$this->workFolder."/signature'".
             " -outform DER -passin pass:'$pass'");
    }

        //signature debugging
       //print(file_get_contents($this->workFolder."/signature"));

    //create the zip bundle from the pass files
    function writePassBundle() {  
      //1 generate the name for the .pkpass file
      $passFile = $this->workFolder."/".$this->ID.".pkpass";

      //2 create Zip class instance
      $zip = new ZipArchive();
      $success = $zip->open($passFile, ZIPARCHIVE::OVERWRITE);
      if ($success!==TRUE) die("Can't create file $passFile");

      //3 recurse over contents and build the list
      $files = new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($this->workFolder), 
                 RecursiveIteratorIterator::SELF_FIRST);

      //4 add files to the archive
      foreach($files as $name => $fileObject){
        if (is_file($name) && 
            substr($fileObject->getFileName(), 0, 1)!=".") {

          $relativeName = str_replace($this->workFolder."/", 
                                      "",$name);
          $zip->addFile($fileObject->getRealPath(), $relativeName);
        }
      }

      //5 close the zip file
      $zip->close();  

      //6 save the .pkpass file path and return it too
      $this->passBundleFile = $passFile;
      return $passFile;
    }

    //make new instance from a source folder
    function __construct($path) {
      assert(file_exists($path."/pass.json"));

      $this->ID = uniqid();

      $this->workFolder = sys_get_temp_dir()."/".$this->ID;
      mkdir($this->workFolder);
      assert(file_exists($this->workFolder));

      $this->copySourceFolderFilesToWorkFolder($path);

      $this->readPassFromJSONFile($this->workFolder."/pass.json");  
    }

    //delete all auto-generated files in the temp folder
    function cleanup()
    {
      //recurse over contents and delete files
      $files = new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($this->workFolder), 
                 RecursiveIteratorIterator::CHILD_FIRST);

      foreach($files as $name => $fileObject){
        if (is_file($name)) {
          unlink($name);
        } else if (is_dir($name)) {
          rmdir($name);
        }
      }

      rmdir($this->workFolder);
    }

    //cleanup the temp folder on object destruction
    function __destruct() {
      $this->cleanup();
    }

        **/* THIS I ADDED AS A NEW FUNCTION TO PRESENT/DOWNLOAD */
        function outputPassBundleAsWebDownload($fileName) {
         //dump the generated pass to the browser
        header("Content-Type: application/vnd.apple.pkpass");
        header("Content-Disposition: attachment; ".
         "filename=".basename($this->$fileName));
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ". filesize($this->$fileName));
        flush();
        readfile($this->$fileName);**
}
}

?>

问题是通行证没有被出示。它被正确创建是因为我在屏幕上得到了带有文件名的回声。我错过了什么?我从 getPass.php 调用输出函数并传入 $fileName。为什么它不起作用?

4

1 回答 1

1

您需要注释掉回显文件名的行。将内容回显到浏览器会强制 PHP 自动生成文本输出的标题,因此它可以提供文件名的文本。

一旦开始向浏览器输出,您就无法发送额外的标头。这就是为什么您的输出通行证捆绑函数中的标头被忽略并且您的通行证没有被下载的原因。

于 2013-04-13T01:03:47.353 回答