BloggerAds廣告

相關文章

顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2017年2月3日 星期五

XAMPP Apache Custom Directory list

To modify the default ugly directory list, normaly we need to modify mod_autoindex and recompile blar blar.

For me, i have no any knowledge of C, such actions will drive me nuts.

We are going to re write the directory using PHP, CSS & HTML. no C knowledge required

Here are the summary of what we'll doing:

1. Config .htaccess file

2. Prepare html assets

3. Hide original Directory output

4. Inject our php code

For easier to explain, i will create everything in root folder (rootFolder)

the default root folder of xampp should be c:/xampp/htdocs/
create a folder (custom_dir) in rootFolder which contains your custom html code
create a .htaccess file in rootFolder and copy the following code

<!-- .htaccess -->

# DIRECTORY CUSTOMIZATION 
<IfModule mod_autoindex.c> 
 # SET INDEX OPTIONS - this enable using FancyIndexing and tell apache we will use our own html to generate the directory list
 IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* SuppressHTMLPreamble

 # SPECIFY HEADER FILE - tell Apache where our header file stores
 HeaderName /custom_dir/header.php 

 # SPECIFY FOOTER FILE - tell Apache where our footer file stores (yes, we use ReadmeName as our footer, since ReadmeName will keep generated at the bottom)
 ReadmeName /custom_dir/footer.html 

 # IGNORE THESE FILES - hide our html asset file in directory list
 IndexIgnore /custom_dir 
</IfModule>
    

create a header.php into custom_dir, we will write our own code to generate the list
<!-- header.php -->   

<?php
$serverRoot = $_SERVER['DOCUMENT_ROOT'];
$cssPath = $serverRoot.'/custom_dir/css.php';

$currentPath = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']; //always get the 'current' folder
$dirList = new DirectoryIterator(str_replace('/', '\\', $currentPath)); //just replace front-slash in case there're
?>
 
<html> 
 <head> 
  <title>Directory Title</title>
  
  <!-- make our directory responsive -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <style>
   <?php include_once($cssPath); //hide our system path?> 
  </style>
 </head>

 <body>
  <header>
   <h1>Directory Title</h1>
  </header>

  <div>
   <table class='dirTable'> 
    <tr>
     <th>Name</th>
     <th>Last modified</th>
    </tr> 

    <?php foreach($dirList as $fileInfo): ?>
    <tr class='dirItem'>
     <td> <a href='<?=$fileInfo->getFilename() ?>'><?=$fileInfo->getFilename() ?></a> </td>
     <td> <?=date( "Y-m-d H:i", $fileInfo->getMTime()) ?> </td>
    </tr>
    <?php endforeach; ?>
   </table>
  </div>
    

then create the footer.php and do whatever you like
<!-- footer.php -->
   <footer class='yourFooterClass'>
    YOUR OWN FOOTER HTML CODE
   </footer>
 </body>
</html>
  
next, create css.php, by doing this instead of <link> is to hide the source path
/*the default list is generated by pre, we simply hide this by applying this css rule*/
pre { display:none; !important};

/*then do whatever you like*/ 
  

2016年1月7日 星期四

remove  BOM

http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype

credits to Jasonhao

//Remove UTF8 Bom

function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}

2015年6月26日 星期五

php pass data to include_once() require_once()

The following code shows how to pass params to included files in php
it works also in the following functions:
include()
include_once()
require()
require_once()


application.php
Using an Associative array to carry variables would be an ideal option.
<?php
$m = new Main();

$items = array(
 'name'=> 'John Doe',
 'items'=>array('stone', 'knife', 'card', 'etc...')
);

$m->loadViewWithParams('item_list_view.php', $items);
?>

Main.php
For variables which visibles to the line above the include(), they also visibles to the included file.
We simply create variables just above the include() just the way that many people do so.
Since we've passed an associative array to loadViewWithParams(), we will break the array into variables in runtime.
<?php
class Main {
/**
*
*@param $path string the file path to load.
*@param $params array an associative array which carry data to the loaded file.
*/
  public function loadViewWithParams($path, $params = array()){
    //we use a foreach to parse the array to variables
    foreach ($params as $key => $value ) {
      $$key = $value;
    }

    include_once($path);
  }
}
?>

item_list_view.php
consider the application.php above
the loadViewWithParams() method converted the array into variables
now both $name and $item were passed to the included file
<div>
 <h1>Welcome, <?php echo $name; ?> </h1>
 here are your items
 <ul>
  <?php foreach($myItemList as $item): ?>
  <li><?php echo $item; ?></li>
  <?php endforeach; ?>
 <ul>
</div>

2014年10月3日 星期五

php email 標題 主旨 寄件者 亂碼問題

<?php
function emailEncode($str) {
  return "=?UTF-8?B?".base64_encode($str)."?=";
}

$subject = emailEncode("hello world");
$sender = emailEncode("littleshell");
?>