2016-08-03 26 views
0

我是一個新的開發,並已最近完成編碼了一個簡單的PHP MySQL的數據庫管理系統爲我公司。現在我已經完成讓它開始工作,我想讓它更安全,實現我可以在網上找到的所有安全最佳實踐(例如:正則表達式,在web目錄之外有mysqli_connect.php與腳本內的連接字符串等)在我上網之前。我已經編輯了一些此代碼之外的個人信息,並將其替換爲它的工作內容,但括號內。如果可能的話,我不想讓它在網上發佈。引用到mysqli_connect.php休息PHPExcel下載

通常情況下,我能夠用我的數據庫的工作只是一個參考mysqli_connect.php,像這樣:

require_once ('..\mysqli_connect.php');

一個對我的DBMS的功能是「下載到Excel」按鈕。它完美的工作,但只有當我在文件內直接連接。如果我試圖像我在上面的例子做寫我的連接,Excel文件顯示以下錯誤消息:

Excel cannot open the file 'xyz.xlsx' because the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

當我直接寫的連接ExcelDownload.php裏面,Excel文件正確下載。連接字符串看起來是這樣的:

$dbc = @mysqli_connect('[myhost]', '[myusername]', '[mypassword]', '[mydatabase]')

如此反覆,基本上,它引用了連接間接引起我的Excel下載到錯誤了,我不知道爲什麼。如果我直接召喚連接,它工作正常。任何幫助將不勝感激。再次

感謝,Jibreel

這裏是ExcelDownload.php,順便說一句:

<?php 
session_start(); 

//VERIFY LOGGED IN: Redirects user back to home page if they are not logged in or if they don't have the right privileges. Should be atop every page. 
if(!isset($_SESSION['id'])) 
{header("Location: login.php");} 

//CALLS PHPEXCEL: Creates connection to PHPExcel class library, and creates new instance of PHPExcel. 
//    From there it sets the attributes for some of PHPExcel's methods, to explain how we want the spreadsheet to be designed. 

require_once 'Classes/PHPExcel.php'; 
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->getActiveSheet()->setTitle('OpenOrders'); 
$objPHPExcel->getActiveSheet()->setCellValue('A1', '[my company]'); 

//CONFIRMATION: If Export to Excel button is pushed, then do this 
if(isset($_POST["export_Excel"])) 
{ 


    //DANGER!! UNSECURED DATABASE CONNECTION. BAD PRACTICE. CHANGE IN NEXT VERSION! 
    //GENERATE QUERY: Connects to database -- Once connected, runs select * query and saves the outcome in $result 
    $dbc = @mysqli_connect('[myhost]', '[myusername]', '[mypassword]', '[mydb]') 
    OR die ('Could not connect to MySQL ' . mysqli_connect_error()); 
    $sql = "[myquery]"; 
    $result = mysqli_query($dbc, $sql); 

    //SET HEADING VALUE: Sets the values for the top row of the spreadsheet, which will be the headings. 
    $objPHPExcel->getActiveSheet()->setCellValue('C3', 'OOPONO'); 
    $objPHPExcel->getActiveSheet()->setCellValue('D3', 'Order Status'); 
    $objPHPExcel->getActiveSheet()->setCellValue('E3', 'Order Comments'); 




    //ITERATOR: If there are values inside of $result, starting at row 4, insert values for OOPONO, order status, and comments until $results is totally intereated. 
    if(mysqli_num_rows($result) > 0) 
     { 

      $rownumber = 4; 

      while ($row = mysqli_fetch_array($result)) 
      { 
       $row1 = 'C'.$rownumber; 
        $objPHPExcel->getActiveSheet()->setCellValue($row1, $row["OOPONO"]); 
       $row1 = 'D'.$rownumber; 
        $objPHPExcel->getActiveSheet()->setCellValue($row1, $row["Order_Status"]); 
       $row1 = 'E'.$rownumber; 
        $objPHPExcel->getActiveSheet()->setCellValue($row1, $row["Comments"]); 


       $rownumber = $rownumber + 1; 
      }      
     } 
} 

//DOWNLOAD SETUP: Defines the different attributes of the Excel doc ranging from filename to extension. It also does the finishing touches of setting up the download. 
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
header('Content-Disposition: attachment;filename="OpenOrders.xlsx"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save('php://output'); 

?> 

回答

1

如此反覆,基本上,它引用了連接間接導致我的Excel下載到錯誤了,我不要不知道爲什麼。如果我直接召喚連接,它工作正常。

沒有! referencing the connection indirectly causes發生錯誤,這會導致發送到您的顯示器的錯誤消息;然後Excel輸出也會發送到您的顯示器,以便錯誤消息成爲Excel數據流內容的一部分,因此錯誤消息被注入到文件中,導致文件損壞。

在文本編輯器中的文件,你應該能夠看到你越來越

+0

我打開該文件,當時只有一羣完全隨機的字符實際的錯誤信息。我嘗試將擴展名更改爲.txt,但仍得到相同的結果。結果太長,無法在這裏發佈。你能幫我知道從哪裏出發? 謝謝:-) –

+0

我不能幫你輕鬆.....我已經告訴過你什麼是問題,但我沒有訪問你的日誌,你的文件等,指向你到實際行 –

+0

好吧,我會盡我所能,然後嘗試自己解決它。謝謝你:) –