dongqian3198 2016-10-11 05:10
浏览 83
已采纳

在PHPExcel中使用isset值

I'm currently using an isset to get the username of the user logged in to save the Excel document to there area in my site directory. Now I am also using a second isset for an encryption so the user cannot simply change the username isset and save there files to someone else's directory unless they know their encryption key as well which changes daily, or on press. Now when I try I get an error message, now i'm not sure if it's the isset not working with it or if it's the documents source files to create the document. Anyone have any ideas on how to fix this?

Error

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

  1. <?php
  2. if (isset($_GET['logout'])){
  3. session_destroy();
  4. header("Location: index.php");
  5. exit();
  6. }
  7. else{
  8. if (isset($_GET['Username'])){
  9. if (isset($_GET['Encryption'])){
  10. $Username = $_GET['Username'];
  11. $Encryption = $_GET['Encryption'];
  12. if (is_dir("USERS/".$Username) === true) {
  13. if($Encryption == file_get_contents("USERS/".$Username."/Encryption.txt")){
  14. $NoOfDocs = file_get_contents("USERS/".$Username."/NoOfDocuments.txt");
  15. }
  16. else{
  17. header("Location: 201Error.php");exit();
  18. }
  19. }
  20. else{
  21. header("Location: 201Error.php");exit();
  22. }
  23. }
  24. }
  25. }
  26. //Excel Transfer
  27. $myFile=fopen("USERS/".$Username."/RiskAssessment/A1.txt","r") or exit("Can't open file!");
  28. $A1 = fgets($myFile);
  29. if ($Spo === false) $A1 = '';
  30. fclose($myFile);
  31. $myFile=fopen("USERS/".$Username."/RiskAssessment/A2.txt","r") or exit("Can't open file!");
  32. $A2 = fgets($myFile);
  33. if ($Spo === false) $A2 = '';
  34. fclose($myFile);
  35. $myFile=fopen("USERS/".$Username."/RiskAssessment/A3.txt","r") or exit("Can't open file!");
  36. $A3 = fgets($myFile);
  37. if ($Spo === false) $A3 = '';
  38. fclose($myFile);
  39. $myFile=fopen("USERS/".$Username."/RiskAssessment/A4.txt","r") or exit("Can't open file!");
  40. $A4 = fgets($myFile);
  41. if ($Spo === false) $A4 = '';
  42. fclose($myFile);
  43. $myFile=fopen("USERS/".$Username."/RiskAssessment/A5.txt","r") or exit("Can't open file!");
  44. $A5 = fgets($myFile);
  45. if ($Spo === false) $A5 = '';
  46. fclose($myFile);
  47. $Date = date("d/m/Y");
  48. Include_once 'PHP/PHPExcel.php';
  49. $objPHPExcel = new PHPExcel();
  50. $activeSheet = $objPHPExcel->getActiveSheet();
  51. $objPHPExcel->getActiveSheet()->setTitle('Risk Assessment');
  52. $border = array(
  53. 'borders' => array(
  54. 'allborders' => array(
  55. 'style' => PHPExcel_Style_Border::BORDER_THIN
  56. )
  57. )
  58. );
  59. //Set Up
  60. $activeSheet->setCellValue('A1','Risk Assessment');
  61. $activeSheet->setCellValue('F1',$Date);
  62. $objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
  63. $objPHPExcel->getActiveSheet()->mergeCells('F1:I1');
  64. //Title
  65. $title = array(
  66. 'font' => array(
  67. 'bold' => true,
  68. 'size' => 32,
  69. ));
  70. $horizontalCenter = array(
  71. 'alignment' => array(
  72. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  73. )
  74. );
  75. $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($title);
  76. //Basic Information
  77. $objPHPExcel->getActiveSheet()->mergeCells('B2:D2');
  78. $objPHPExcel->getActiveSheet()->mergeCells('A3:I3');
  79. $objPHPExcel->getActiveSheet()->mergeCells('A4:I4');
  80. $objPHPExcel->getActiveSheet()->mergeCells('A5:I5');
  81. $objPHPExcel->getActiveSheet()->mergeCells('A6:I6');
  82. $objPHPExcel->getActiveSheet()->mergeCells('A7:I7');
  83. $objPHPExcel->getActiveSheet()->mergeCells('A8:I8');
  84. $objPHPExcel->getActiveSheet()->mergeCells('A9:I9');
  85. $objPHPExcel->getActiveSheet()->mergeCells('A10:I10');
  86. $objPHPExcel->getActiveSheet()->mergeCells('F11:G11');
  87. $objPHPExcel->getActiveSheet()->mergeCells('H11:I111');
  88. $objPHPExcel->getActiveSheet()->mergeCells('F12:G12');
  89. $objPHPExcel->getActiveSheet()->mergeCells('H13:I13');
  90. $activeSheet->setCellValue('B2','Risk No.1');
  91. $activeSheet->setCellValue('A3','What are the hazards?');
  92. $activeSheet->setCellValue('A4', $A1);
  93. $activeSheet->setCellValue('A5','What are you already doing?');
  94. $activeSheet->setCellValue('A6',$A2);
  95. $activeSheet->setCellValue('A7','Do you need to do anything else to control this risk?');
  96. $activeSheet->setCellValue('A8',$A3);
  97. $activeSheet->setCellValue('A9','Action by who?');
  98. $activeSheet->setCellValue('A10',$A4);
  99. $activeSheet->setCellValue('F11','Action by when?');
  100. $activeSheet->setCellValue('F12',$A5);
  101. $activeSheet->setCellValue('H11','Done');
  102. $activeSheet->setCellValue('H12',$A6);
  103. //Document Settings
  104. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  105. $objWriter->save('USERS/".$Username."/RiskAssessment/Risk_Assessment.xls');
  106. ?>

展开全部

  • 写回答

1条回答 默认 最新

  • doushitang4276 2016-10-11 05:19
    关注

    Change this line

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

    to this

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

    Or this

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

    Or this

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

    Note the quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部