2016-10-11 61 views
0

我目前使用isset來獲取登錄用戶的用戶名,以便將Excel文檔保存到我的站點目錄中的區域。現在我還使用第二個isset進行加密,因此用戶不能簡單地更改用戶名isset並將文件保存到其他人的目錄,除非他們知道每天或在印刷機上更改其加密密鑰。現在,當我嘗試獲取錯誤消息時,現在我不確定是否isset不適用於它或者它是用於創建文檔的文檔源文件。任何人有任何想法如何解決這個問題?在PHPExcel中使用isset值

錯誤

Warning: fopen(USERS/".$Username."/Documents/DocName.xls) [function.fopen]: failed to open stream: No such file or directory in public_html/Site/PHP/PHPExcel/Shared/OLE/PPS/Root.php on line 90

Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Can't open USERS/".$Username."/Documents/DocName.xls. It may be in use or protected.' in /public_html/Site/PHP/PHPExcel/Shared/OLE/PPS/Root.php:93 Stack trace: 0 /public_html/Site/PHP/PHPExcel/Writer/Excel5.php(226): PHPExcel_Shared_OLE_PPS_Root->save('USERS/".$Userna...') #1 /public_html/Site/Create.php(124): PHPExcel_Writer_Excel5->save('USERS/".$Userna...') #2 {main} thrown in /public_html/SitePHP/PHPExcel/Shared/OLE/PPS/Root.php on line 93

PHP

<?php 
if (isset($_GET['logout'])){ 
    session_destroy(); 
    header("Location: index.php"); 
    exit(); 
} 
else{ 
    if (isset($_GET['Username'])){ 
     if (isset($_GET['Encryption'])){  
      $Username = $_GET['Username']; 
      $Encryption = $_GET['Encryption']; 

      if (is_dir("USERS/".$Username) === true) { 
       if($Encryption == file_get_contents("USERS/".$Username."/Encryption.txt")){ 
        $NoOfDocs = file_get_contents("USERS/".$Username."/NoOfDocuments.txt"); 
       } 
       else{ 
        header("Location: 201Error.php");exit();  
       } 
      } 
      else{ 
       header("Location: 201Error.php");exit();  
      } 
     } 
    } 
} 

//Excel Transfer 
    $myFile=fopen("USERS/".$Username."/RiskAssessment/A1.txt","r") or exit("Can't open file!"); 
    $A1 = fgets($myFile); 
    if ($Spo === false) $A1 = ''; 
    fclose($myFile); 

    $myFile=fopen("USERS/".$Username."/RiskAssessment/A2.txt","r") or exit("Can't open file!"); 
    $A2 = fgets($myFile); 
    if ($Spo === false) $A2 = ''; 
    fclose($myFile); 

    $myFile=fopen("USERS/".$Username."/RiskAssessment/A3.txt","r") or exit("Can't open file!"); 
    $A3 = fgets($myFile); 
    if ($Spo === false) $A3 = ''; 
    fclose($myFile); 

    $myFile=fopen("USERS/".$Username."/RiskAssessment/A4.txt","r") or exit("Can't open file!"); 
    $A4 = fgets($myFile); 
    if ($Spo === false) $A4 = ''; 
    fclose($myFile); 

    $myFile=fopen("USERS/".$Username."/RiskAssessment/A5.txt","r") or exit("Can't open file!"); 
    $A5 = fgets($myFile); 
    if ($Spo === false) $A5 = ''; 
    fclose($myFile); 

    $Date = date("d/m/Y"); 


    Include_once 'PHP/PHPExcel.php'; 

    $objPHPExcel = new PHPExcel(); 
    $activeSheet = $objPHPExcel->getActiveSheet(); 
    $objPHPExcel->getActiveSheet()->setTitle('Risk Assessment'); 

    $border = array(
     'borders' => array(
      'allborders' => array(
       'style' => PHPExcel_Style_Border::BORDER_THIN 
       ) 
      ) 
     ); 

//Set Up 
    $activeSheet->setCellValue('A1','Risk Assessment'); 
    $activeSheet->setCellValue('F1',$Date); 
    $objPHPExcel->getActiveSheet()->mergeCells('A1:E1'); 
    $objPHPExcel->getActiveSheet()->mergeCells('F1:I1'); 

//Title 
    $title = array(
     'font' => array(
      'bold' => true, 
      'size' => 32, 
     )); 

    $horizontalCenter = array(
     'alignment' => array(
      'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 
     ) 
    ); 

    $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($title); 

//Basic Information 
    $objPHPExcel->getActiveSheet()->mergeCells('B2:D2'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A3:I3'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A4:I4'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A5:I5'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A6:I6'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A7:I7'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A8:I8'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A9:I9'); 
    $objPHPExcel->getActiveSheet()->mergeCells('A10:I10'); 
    $objPHPExcel->getActiveSheet()->mergeCells('F11:G11'); 
    $objPHPExcel->getActiveSheet()->mergeCells('H11:I111'); 
    $objPHPExcel->getActiveSheet()->mergeCells('F12:G12'); 
    $objPHPExcel->getActiveSheet()->mergeCells('H13:I13'); 

    $activeSheet->setCellValue('B2','Risk No.1'); 
    $activeSheet->setCellValue('A3','What are the hazards?'); 
    $activeSheet->setCellValue('A4', $A1); 
    $activeSheet->setCellValue('A5','What are you already doing?'); 
    $activeSheet->setCellValue('A6',$A2); 
    $activeSheet->setCellValue('A7','Do you need to do anything else to control this risk?'); 
    $activeSheet->setCellValue('A8',$A3); 
    $activeSheet->setCellValue('A9','Action by who?'); 
    $activeSheet->setCellValue('A10',$A4); 
    $activeSheet->setCellValue('F11','Action by when?'); 
    $activeSheet->setCellValue('F12',$A5); 
    $activeSheet->setCellValue('H11','Done'); 
    $activeSheet->setCellValue('H12',$A6); 

//Document Settings 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
    $objWriter->save('USERS/".$Username."/RiskAssessment/Risk_Assessment.xls'); 
?> 
+0

可能重複的[PHP - 無法打開流:沒有這樣的文件或目錄](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or -directory) –

回答

1

改變這一行

$objWriter->save('USERS/".$Username."/RiskAssessment/Risk_Assessment.xls'); 

這個

$objWriter->save("USERS/".$Username."/RiskAssessment/Risk_Assessment.xls"); 

或者這

$objWriter->save('USERS/'.$Username.'/RiskAssessment/Risk_Assessment.xls'); 

或者這

$objWriter->save("USERS/$Username/RiskAssessment/Risk_Assessment.xls"); 

注意引號。

+0

這很好用,謝謝你發現這個。 –