PHP& ZipArchive() - 无法加载PDF文档

时间:2018-03-27 18:13:48

标签: javascript php html pdf ziparchive

晚上好,我有一个表格,在确定的字段中打开一个窗口或窗口来显示特定的PDF。路径所在的文件夹,根据输入的填充值,它可以只有PDF或ZIP文件,其中包含多个PDF内容。

这是我的代码的blur()事件部分,它调用窗口并将值传递给" arquivo.php"和" pdf.php",一个是检查是否只是一个PDF或ZIP,如果是PDF这个返回我的表单通知这是一个PDF只是打开后只是一个窗口显示,这部分还可以

JavaScript / HTML代码:

///////////PASS THE VALUES TO "arquivo.php"////////////////
     $.post("arquivo.php", {nprocesso: document.getElementById("processo").value}, function(data){
        $( "#content3" ).html( data ); /*<-- return in this content if is a PDF or a ZIP(when is a zip it returns how many PDF's there are inner ZIP file too)*/

    ////////////THIS PART IS TO CREATE JUST A WINDOW OR MULTIPLES WINDOWS PASSING THE VALUES RETURNED IN THE "content3" TO "pdf.php" TO DISPLAY IT/THEM//////////////////    
    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");    
                  }
               }
              document.getElementById('closeBtn').click();
              }, 1000);
            }

这是&#34; arquivo.php&#34;,其中检查是否只是PDF或ZIP文件,并提取if是否为zip

Arquivo.php代码:

<?php
  session_start();

  //////////THIS PART CHECK THAT FILE IS A PDF AND RETURN TO MY JAVASCRIPT CODE/////////
  if(file_exists('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'].'.pdf')){

    echo 'PDF';
    $_SESSION['file'] = "PDF";

  }else{

//////////THIS PART CHECK THAT FILE IS A ZIP AND RETURN TO MY JAVASCRIPT CODE/////////

    echo 'ZIP';
    $_SESSION['file'] = "ZIP";
     $zip = new ZipArchive;
     $path = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'].'.zip';
     ///////////////////////////////////////////////////////////////////

      if($zip->open($path) === true){
       //////////THIS PART IT OPENS THE ZIP AND EXTRACT/////////
if(!file_exists('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'])){
            mkdir('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'], 0777, true);
           }
            for($i = 0; $i < $zip->numFiles; $i++){
             $zip->extractTo('//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'], array($zip->getNameIndex($i)));
             $path = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'];
             $files = scandir($path); array_shift($files); array_shift($files);
             $file = array(); $filename = array();
        ///////NOW I CATCH THE PDF's NAMES AND THE QUANTITY AND RETURN TO JAVASCRIPT CODE///////
             $file[$i] = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'].'/'.$files[$i];
             $filename[$i] = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'].'/'.$files[$i];
             $nomes_pdfs = array();
             $nomes_pdfs[$i] = substr(substr($filename[$i], strpos($filename[$i], $_POST['nprocesso'])+26, strlen($filename[$i])), 0, strpos(substr($filename[$i], strpos($filename[$i], $_POST['nprocesso'])+26, strlen($filename[$i])), '.pdf')).',';
             echo $nomes_pdfs[$i];
            }               
           $zip->close();

           $path = '//dsbimrj16/Vinculacao_Cadastro_Gestor/'.$_POST['nprocesso'];
           $files = scandir($path);
           array_shift($files); array_shift($files); $_SESSION['quantidade_pdf'] = count($files);
           echo $_SESSION['quantidade_pdf'];
        }
      }
    ?>

我的问题实际上部分在&#34; pdf.php&#34;其中窗口加载了&#34; content3&#34;的返回值。如果是单个PDF,则窗口显示正常,但是当具有多个PDF的ZIP文件时,第一个PDF显示正确,但从第二个开始,然后开启,它不会显示。显示此错误:&#34;无法加载PDF文档&#34;。

pdf.php代码:

<?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>');
 }
}
?>

我做错了什么?我已经检查了文件夹,文件已经存在,名称和路径与代码显示的相同

0 个答案:

没有答案