PHP - 如何使用ZipArchive()显示另一个PDF页面

时间:2018-03-23 20:24:31

标签: php ziparchive

我现在的问题是,第二个PDF和on,它没有显示,'导致错误。 “无法加载PDF文档”。我该如何解决这个问题?

<?php
header ('Content-type: text/html; charset=utf-8');
if(file_exists('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1).'.pdf') && is_file('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1).'.pdf')){
 $processo = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1);
 $file = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$processo.'.pdf';
 $filename = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$processo.'.pdf';
 header('Content-type: application/pdf');
 header('Content-Disposition: inline; filename="' . $filename . '"');
 header('Content-Transfer-Encoding: binary');
 header('Content-Length: ' . filesize($file));
 header('Accept-Ranges: bytes');
 @readfile($file);
}else{
/////////////////////HERE IS THE PART WHEN IS A ZIP////////////////////////
///////*This part it's only to check if the folder extract exists*/////////
 function folder_exist($folder){ $path = realpath($folder); return ($path !== false AND is_dir($path)) ? $path : false; }
 chdir('//dsbimrj16/Vinculacao_Cadastro_Gestor/');
 $folder = '/'.substr(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'p/')+2), 0, strpos(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'p/')+2), '/')).'/';
 if(FALSE !== ($path = folder_exist($folder))){
 /////*Here start the part where the code catch the PDF's and display*/////
  $pasta = substr(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'p/')+2), 0, strpos(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'p/')+2), '/'));
  $processo = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1);
  $file = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$pasta.'/'.$processo.'.pdf';
  $filename = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$pasta.'/'.$processo.'.pdf';
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: ' . filesize($file));
  header('Accept-Ranges: bytes');
  @readfile($file);
 }else{
  die('<h2 style="background-color:#FA5858"><center>Não foi encontrado a inicial do processo. Verifique se o mesmo encontra-se na pasta.</center></h2>');
 }
}
?>

这是我的Javascript / HTML代码,它将PDF的名称传递给PHP文件以打开它们:

  setTimeout(function(){

   if(document.getElementById("content3").innerText == "PDF"){
      window.open("pdf.php/"+document.getElementById("processo").value, '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800"); 
   }else{
      var QuantPDF = Number(document.getElementById("content3").innerText.slice(Number(document.getElementById("content3").innerText.lastIndexOf(','))+1, document.getElementById("content3").innerText.length));//Number(document.getElementById("content3").innerText.replace(/[^0-9]/g, ""));
      var NomesPDFS = []; NomesPDFS = document.getElementById("content3").innerText.slice(3, document.getElementById("content3").innerText.length-2).split(',');
      if(QuantPDF > 1){
       for(var i = 0; i < QuantPDF; i++){
        window.open("pdf.php/"+document.getElementById("processo").value+"/"+NomesPDFS[i], '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800");  
       }   
      }else{
       window.open("pdf.php/"+document.getElementById("processo").value, '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800");    
      }
   }

  }, 1000);

1 个答案:

答案 0 :(得分:0)

将变量($ i)传递给php脚本,以便它可以跟踪PDF数组中的位置:

$.post("arquivo.php", {nprocesso: document.getElementById("processo").value}, function(data){   $( "#content3" ).html( data ); });

setTimeout(function(){

if(document.getElementById("content3").innerText == "PDF"){ //if the file is just a PDF, open it
  window.open("pdf.php/"+document.getElementById("processo").value, '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800"); 
}else{ //if the file is a zip file, or many zip files, pass them to zip.php and unpack them
  var QuantPDF = document.getElementById("content3").innerText.replace(/[a-zA-Z]/g, "");
   if(QuantPDF > 1){  //there is more than one zip file
    for(var i = 0; i < QuantPDF; i++){ //loop the zip files
  window.open("zip.php/"+document.getElementById("processo").value + "/" + $i, '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800");  
} 
   }else{ //Open just the one zip file
    window.open("zip.php/"+document.getElementById("processo").value, '_blank', "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800");    
   }
}

}, 1000);

然后从PHP文件中删除循环,因为每次弹出一次只能获得一个循环:

<?php
$urlOfPHPFile = "http://somedomainthatsawesome.com/zip.php/somefilename.zip/2";
$arrURI = explode("/",$urlOfPHPFile);
$pdfPosition = array_pop($arrURI);
$zipFilename = array_pop($arrURI); //$processo
var_dump($pdfPosition);    //obviously comment this out when you're ready to test
echo "<br/>";    //obviously comment this out when you're ready to test
var_dump($zipFilename);    //obviously comment this out when you're ready to test
echo "<hr/>";    //obviously comment this out when you're ready to test
//....
$file = array($pdfPosition);
$stat = $za->statIndex( $pdfPosition );
$file[$pdfPosition] = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.basename($stat['name']).PHP_EOL;
$filename = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.basename($stat['name']).PHP_EOL;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file[$pdfPosition] . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file[$pdfPosition]));
header('Accept-Ranges: bytes');
@readfile($file[$pdfPosition]);
print_r( basename( strtoupper($stat['name']) ) . PHP_EOL . '<br>' );
?>