代码如下 | 复制代码 |
# 查找
return strftime($date, $timestamp); # 替换成如下代码:
if(extension_loaded('gettext')) return strftime($date, $timestamp); # 中国区这样设置.
date_default_timezone_set('UTC'); return gmdate('Y-m-d H:i:s', $timestamp + 28800); |
最初的做法
代码如下 | 复制代码 |
include 'classes/PHPExcel/IOFactory.php'; $inputFileName = $target;
$objPHPExcel = $objReader->load($inputFileName); |
报错
PHPExcel_Reader_Exception: The filename .1443578799.xlsx is not recognised as an OLE file in Classes\PHPExcel\Shared\OLERead.php on line 89
( ! ) Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'The filename 1443578799.xlsx is not recognised as an OLE file' in Classes\PHPExcel\Shared\OLERead.php on line 89
( ! ) PHPExcel_Reader_Exception: The filename 1443578799.xlsx is not recognised as an OLE file in Classes\PHPExcel\Shared\OLERead.php on line 89
Call Stack
碰到这个问题我们是因为xlsx出错了,可能通过类型判断 解决办法如下
代码如下 | 复制代码 |
if( $extension =='xlsx' ) |
这样再操作会发现问题已经解决了。
在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。
解决 办法一,file_put_contents 高并发与独占锁定
发现高并发访问时使用 file_put_contents 写入文件造成数据置空。
查看官方文档:
int file_put_contents ( string $filename , string $data [, int $flags [, resource $context ]] )
参数:
filename 要被写入数据的文件名。 data 要写入的数据。类型可以是 string,array 或者是 stream 资源(如上面所说的那样)。 flags flags 可以是 FILE_USE_INCLUDE_PATH,FILE_APPEND 和/或 LOCK_EX(获得一个独占锁定),然而使用 FILE_USE_INCLUDE_PATH 时要特别谨慎。 context 一个 context 资源。
直接直至 flags 参数为 LOCK_EX 即可在高并发时获得一个独占锁定。
另外,flock 函数的也提供了文件锁定方法:
$fp = fopen("/tmp/lock.txt", "w+"); if (flock($fp, LOCK_EX)) { // 进行排它型锁定 fwrite($fp, "Write something here\n"); flock($fp, LOCK_UN); // 释放锁定 } else { echo "Couldn't lock the file !"; } fclose($fp);
注意 flock() 需要一个文件指针。
方法二,file_put_contents() 存在高并发问题,Smarty中处理方式如下。
<?php define("FILE_PUT_CONTENTS_ATOMIC_TEMP", dirname(__FILE__)."/cache"); define("FILE_PUT_CONTENTS_ATOMIC_MODE", 0777); function file_put_contents_atomic($filename, $content) { $temp = tempnam(FILE_PUT_CONTENTS_ATOMIC_TEMP, 'temp'); if (!($f = @fopen($temp, 'wb'))) { $temp = FILE_PUT_CONTENTS_ATOMIC_TEMP . DIRECTORY_SEPARATOR . uniqid('temp'); if (!($f = @fopen($temp, 'wb'))) { trigger_error("file_put_contents_atomic() : error writing temporary file '$temp'", E_USER_WARNING); return false; } } fwrite($f, $content); fclose($f); if ( !@rename($temp , $filename)) { @unlink($filename); @rename($temp, $filename); } @chmod($filename, FILE_PUT_CONTENTS_ATOMIC_MODE); return true; } ?>PHP date()如果要使用我们必须在php.ini中配置时间时区了,如果不设置使用date函数会出现It is not safe to rely on the system s timezone settings错误,下面一起来看看一个例子。
在写php文件上传程序时有时会出现这样的警告:
Upload: 屏幕截图2014-09-28_160214.png
Type: image/png
Size: 66.84765625 Kb
Temp file: D:\Users\Aven\AppData\Local\Temp\php742F.tmp
Warning: move_uploaded_file() [function.move-uploaded-file]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in F:\PHP\upload_file.php on line 26
Warning: move_uploaded_file(upload/屏幕截图2014-09-28_160214.png) [function.move-uploaded-file]: failed to open stream: No such file or directory in F:\PHP\upload_file.php on line 26
Warning: move_uploaded_file() [function.move-uploaded-file]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in F:\PHP\upload_file.php on line 26
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Users\Aven\AppData\Local\Temp\php742F.tmp' to 'upload/屏幕截图2014-09-28_160214.png' in F:\PHP\upload_file.php on line 26
Stored in: upload/屏幕截图2014-09-28_160214.png
这是因为PHP所取的时间是格林威治标准时间,所以和你当地的时间会有出入格林威治标准时间和北京时间大概差8个小时左右,我们可以按照下面的方法解决:
1、在页头使用date_default_timezone_set()设置我的默认时区为北京时间,即 <?php date_default_timezone_set("PRC"); ?>就可以了。
2、在php.ini中设置date.timezone的值为PRC,设置好以后的为:date.timezone=PRC,同时取消这一行代码的注释,即去掉前面的分号就可以了。
这里选用修改配置文件的方式解决问题:
用Notepad++打开php.ini,
查找“date.timezone”,把语句前的分号去掉,值设置为“PRC”,重启FastCGI服务,问题解决
502 bad gateway是php-fpm的问题对于这个问题就是配置参数的问题了,下面我们整理了一些关于php-fpm错误问题的解决办法,具体如下。今天升级完PHP出现了502 Bad Gateway错误,根据经验是php-fpm的问题,但是看到网上那些什么访问量,子进程设置什么的,但是我这太服务器压根没啥流量。所以只能从配置文件下手看看
[global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice [www] listen = /tmp/php-cgi.sock user = www group = www pm = dynamic pm.max_children = 20 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 6 request_terminate_timeout = 100
看到 /tmp/php-cgi.sock 直觉发现应该是个文件,所以前往该目录查看权限,发现文件属主是 root:root,而我的nginx和php-fpm进程是以www用户运行的。所以应该是权限问题。
而配置文件中的user = www,group = www也设置了一个权限,通过 ps -aux|grep php 发现,这是php-fpm进程的属主。
知道问题的源头后便开始查php-fpm的配置,查到了 listen.ower listen.group 设置。
这是改进之后的文件:
[global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice [www] listen = /tmp/php-cgi.sock listen.owner = www listen.group = www user = www group = www pm = dynamic pm.max_children = 20 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 6 request_terminate_timeout = 100
如果不知道查看配置可以尝试下面方法
1、查看当前的PHP FastCGI进程数是否够用:
netstat -anpo | grep "php-cgi" | wc -l
如果实际使用的“FastCGI进程数”接近预设的“FastCGI进程数”,那么,说明“FastCGI进程数”不够用,需要增大。
2、部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间,例如:
...... http { ...... fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; ...... }
......
补充另一篇文章
1.php-fpm进程数不够用
使用 netstat -napo |grep "php-fpm" | wc -l 查看一下当前fastcgi进程个数,如果个数接近conf里配置的上限,就需要调高进程数。
但也不能无休止调高,可以根据服务器内存情况,可以把php-fpm子进程数调到100或以上,在4G内存的服务器上200就可以。
2. 调高调高linux内核打开文件数量
可以使用这些命令(必须是root帐号)
echo 'ulimit -HSn 65536' >> /etc/profile echo 'ulimit -HSn 65536' >> /etc/rc.local source /etc/profile
3.脚本执行时间超时
如果脚本因为某种原因长时间等待不返回 ,导致新来的请求不能得到处理,可以适当调小如下配置。
nginx.conf里面主要是如下
fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;
php-fpm.conf里如要是如下
request_terminate_timeout = 10s
4.缓存设置比较小
修改或增加配置到nginx.conf
proxy_buffer_size 64k; proxy_buffers 512k; proxy_busy_buffers_size 128k; 5. recv() failed (104: Connection reset by peer) while reading response header from upstream
可能的原因机房网络丢包或者机房有硬件防火墙禁止访问该域名
但最重要的是程序里要设置好超时,不要使用php-fpm的request_terminate_timeout,
最好设成request_terminate_timeout=0;
因为这个参数会直接杀掉php进程,然后重启php进程,这样前端nginx就会返回104: Connection reset by peer。这个过程是很慢,总体感觉就是网站很卡。
May 01 10:50:58.044162 [WARNING] [pool www] child 4074, script '/usr/local/nginx/html/quancha/sameip/detail.php' execution timed out (15.129933 sec), terminating
May 01 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal 15 SIGTERM after 90.227060 seconds from start
May 01 10:50:58.046818 [NOTICE] [pool www] child 4082 started
说一千道一万最重要的就是程序里控制好超时,gethostbyname、curl、file_get_contents等函数的都要设置超时时间。