文章标签 » php

PHP中直接读取zip中的指定文件

碉堡。。不愧是世界上最好的语言,只需要一个函数:

file_get_contents('zip://test.zip#test.txt');

来自:http://stackoverflow.com/questions/10420112/how-to-read-a-single-file-inside-a-zip-archive

获取两组数据的固定乱序组合

修改自一个矩阵z形遍历代码,改成从中间开始遍历,乱序效果更好

function matrix_mess_order($rows, $cols) {
    $cuts = $rows + $cols - 1;
    $mid  = (int)($cuts/2);
    $result = array();
    for ($i = 0; $i < $cuts; $i++) {
        if ($i % 2 == 0) {
            $j = $mid - (int)($i % $mid);
        } else {
            $j = $mid + (int)($i % $mid);
        }
        $row_extra = ($j + 1) > $rows ? ($j + 1 - $rows) : 0; 
        $col_extra = ($j + 1) > $cols ? ($j + 1 - $cols) : 0; 
        $len = ($j + 1) - $row_extra - $col_extra;
        $topright_row = $col_extra;
        $topright_col = $j - $col_extra;
        $leftbotm_row = $j - $row_extra;
        $leftbotm_col = $row_extra;
        if ($j % 2 == 0) {
            while ($len -- > 0) {
                $result[] = array($leftbotm_row - $len, $leftbotm_col + $len);
            }
        } else {
            while ($len -- > 0) {
                $result[] = array($topright_row + $len, $topright_col - $len);
            }
        }
    }
    return $result;
}

php-fpm 5.3.3 rpm package with root privileges for centos/redhat 6

出于安全的考虑从源里面安装的php-fpm是不允许以root权限运行的

ERROR: [pool www] please specify user and group other than root

但是有些时候确实需要root权限,可以使用我打的开放root版本

php-fpm-5.3.3-1.i_want_root.el6.i386.rpm
php-fpm-5.3.3-1.i_want_root.el6.x86_64.rpm

如果想打其他php版本的包,可以修改下面的脚本使用

#!/bin/sh
yum install rpm-build gcc make subversion wget
yum install bzip2-devel gmp-devel zlib-devel pcre-devel libxml2-devel openssl-devel
test ! -f ./autoconf-2.13-8.noarch.rpm && wget "http://rpm.bluehua.org/php-fpm/autoconf-2.13-8.noarch.rpm"
rpm -e autoconf
rpm -ivh ./autoconf-2.13-8.noarch.rpm
#修改源码版本
test ! -f ./php-5.3.3.tar.bz2 && wget "http://museum.php.net/php5/php-5.3.3.tar.bz2"
tar jxvf ./php-5.3.3.tar.bz2
cd php-5.3.3/
rm -rf ./sapi/fpm/
#选择适当的php-fpm版本
svn co http://svn.php.net/repository/php/php-src/branches/PHP_5_3_10/sapi/fpm/ ./sapi/fpm
./buildconf --force
test ! -f ./php-fpm-build-rpm.tar.gz && wget "http://rpm.bluehua.org/php-fpm/php-fpm-build-rpm.tar.gz"
tar zxvf ./php-fpm-build-rpm.tar.gz
#修改php-fpm.spec中的版本号
rpmbuild -bb ./php-fpm.spec

一个函数搞定PHP调试日志

对,你没有看错,不是syslog,也不是一坨class,一个函数搞定。。。
—————————-
今天有个搞C++同事问我PHP怎么打印日志,于是贴了他一个函数,绝对实用~
https://gist.github.com/emptyhua/5055043

function debug_log() {
    if (false) return; //开关
    static $fp = 0;
    if ($fp === 0) {
        $logname = 'myprojectname';//日志名称
        $fp = fopen('/tmp/' . $logname . '.debug.log', 'a');
    } 
    $traces = debug_backtrace();
    $trace = count($traces) > 1 ? $traces[1] : $traces[0];
    $log_msg = date('Y-m-d H:i:s') . ' FILE:' . basename($trace['file']) . ' FUNC:' . $trace['function'] . ' LINE:' . $trace['line'] . ' :' . "\n";
    foreach(func_get_args() as $arg) {
        if (is_string($arg)) {
            $log_msg .= $arg . ' ';
        } else {
            $log_msg .= var_export($arg, true) . ' ';
        }
    }
    fwrite($fp, $log_msg . "\n");
}
debug_log('队列ID:', $queue_id, '内容:', $task_infos);
2013-02-28 16:53:40 FILE:task.inc.php FUNC:_get_locate_task LINE:347
队列ID: locate337512971_taskqueue 内容: array (
)

backup:展开文件路径中的通配符

python:

import glob
for input_file in glob.iglob(sys.argv[1]):
    for root, dirs, files in os.walk(input_file):
        for file in files:
            if file.endswith('.js') and not file.endswith('.min.js'):
                process_static_file(os.path.join(root, file))
            elif file.endswith('.css'):
                process_static_file(os.path.join(root, file))

php:

foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

node.js:
https://github.com/isaacs/node-glob

var glob = require("glob")
 
// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
})