Share some of the inquiries and techniques encountered in php work [2]

Keywords: PHP Excel Linux SQL

1 How to define common file separator symbols for linux and window s

DIRECTORY_SEPARATOR :  
The directory separator is the built-in constant that defines php. On debugging machines, we used to use "" as a file separator in windows, but on linux, the system does not recognize this identifier, so we need to introduce the PHP built-in constant: DIRECTORY_SEPARATOR.
So you don't have to decide whether it's on linux or window s.
 
2 PHP Compression and Decompression Classes
PclZip is a powerful PHP class to compress and decompress zip files. PclZip library can compress and decompress ZIP format compressed files (WinZip, PKZIP); and can process such files, including generating compressed files, listing the contents of compressed files and decompressing files. At the same time, you can add or delete files to existing ZIP packages.
It has recently been used in compressing files

Generate zip files

Usage 1:
<?php include_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); $v_list = $archive->create('file.txt,data/text.txt,folder'); if ($v_list == 0) { die("Error : ".$archive->errorInfo(true)); } ?>
Usage two:
<?php include_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); $v_list = $archive->create('data/file.txt,data/text.txt', PCLZIP_OPT_REMOVE_PATH, 'data', PCLZIP_OPT_ADD_PATH, 'install'); if ($v_list == 0) { die("Error : ".$archive->errorInfo(true)); } ?

Official website: http://www.phpconcept.net/pclzip/

 

3 A small piece of "bad" PHP code optimization process, please carefully understand the optimization process

The following section of "bad" PHP code is a simplified test. The question is like: How do you optimize this code?

<?
echo("<p>Search results for query: " .
$_GET['query'] . ".</p>");
?>

The main problem with this code is that it displays the data submitted by the user directly on the web page, resulting in XSS vulnerabilities. In fact, there are many ways to fill this gap. So, what code do we want?

<?
echo("<p>Search results for query: " .
htmlspecialchars($_GET['query']) . ".</p>");
?>

This is the minimum requirement. The XSS vulnerability is filled with the htmlspecialchars function, thus shielding illegal characters.

<?php  
if (isset($_GET['query']))  
{  
   echo '<p>Search results for query: ',  
   htmlspecialchars($_GET['query'], ENT_QUOTES), '.</p>';  
}  
?> 

Then optimize:

<?php  
if (isset($_GET['query']))  
{  
   echo '<p>Search results for query: ',  
   htmlspecialchars($_GET['query'], ENT_QUOTES), '.</p>';  
}  
?> 
  • <? Was replaced by <? Php, which is more in line with the XML specification.
  • Determine whether it is empty before outputting the value of $_GET['query'].
  • The extra parentheses in the echo command have been removed.
  • Strings are qualified by single quotation marks, thus saving PHP time in searching for replaceable variables from strings.
  • Replacing a period with a comma saves echo time.
  • The ENT_QUOTES identifier is passed to the htmlspecialchars function to ensure that single quotes are also escaped. Although this is the most important, it is also a good habit.

4. Export MySQL data to Excel without tripartite class libraries

If you export data frequently, you may encounter the upper limit of exporting data with third-party class libraries. This is a very helpless thing. When more than 20,000 data are exported, it often fails. It's easy to reach the upper limit of PHP memory usage

// output Excel File Header user.csv Change to the file name you want 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="user.csv"'); 
header('Cache-Control: max-age=0'); 
// Get data from the database, in order to save memory, do not read the data to memory at one time, read one line at a time from the handle. 
$sql = 'select * from tbl where ……'; 
$stmt = $db->query($sql); 
// open PHP File handle, php://output Represents direct output to browsers 
$fp = fopen('php://output', 'a'); 
// output Excel Column name information 
$head = array('Full name', 'Gender', 'Age', 'Email', 'Telephone', '……'); 
foreach ($head as $i => $v) { 
    // CSV Of Excel Support GBK Coding, must be converted, otherwise scrambled code 
    $head[$i] = iconv('utf-8', 'gbk', $v); 
} 
// Pass the data through fputcsv Write to file handle 
fputcsv($fp, $head); 
// Counter 
$cnt = 0; 
// every other $limit OK, refresh the output buffer,Don't be too big or too small 
$limit = 100000; 
// Row-by-row fetching of data without wasting memory 
while ($row = $stmt->fetch(Zend_Db::FETCH_NUM)) { 
    $cnt ++; 
    if ($limit == $cnt) { //Refresh the output buffer,Prevent problems caused by too much data 
        ob_flush(); 
        flush(); 
        $cnt = 0; 
    } 
    foreach ($row as $i => $v) { 
        $row[$i] = iconv('utf-8', 'gbk', $v); 
    } 
    fputcsv($fp, $row); 
}

The method here is to use fputcsv to write CSV files and output Excel files directly to the browser.

Posted by mattsutton on Wed, 20 Mar 2019 21:33:27 -0700