最初的做法
代码如下 | 复制代码 |
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' ) |
这样再操作会发现问题已经解决了。
UEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,本文我们解决在同一个页面上如何放多个 UEditor ,然后再用 PHP 接收 UEditor 的值。同一个网页如何放置多个Ueditor?
参考代码如下:
<form method="post" action="save.php">
<script type="text/plain" id="ueditorId_1" name="ueditorName_1" style="width: 400px;height:500px;">编辑器的默认值_1</script>
<script type="text/javascript">
UE.getEditor('ueditorId_1');
</script>
<script type="text/plain" id="ueditorId_2" name="ueditorName_2" style="width: 450px;height:550px;">编辑器的默认值_2</script>
<script type="text/javascript">
UE.getEditor('ueditorId_2');
</script>
</form>
这样,就会在页面中渲染2个Ueditor编辑器。
PHP如何获取多个Ueditor的值?
注意,上面的 ueditorName_1,ueditorName_2 即为表单的Name值,所以服务端PHP获取Ueditor编辑器的值变得很简单,参考代码如下:
$_POST["ueditorName_1"]; // 第一个编辑器的值
$_POST["ueditorName_2"]; // 第二个编辑器的值
解决百度 ueditor v1.4.3 编辑器上传图片失真的bug?
项目中,用到百度ueditor编辑器,发现,上传大一点的图片,图片会失真,刚开始还以为是PHP端做了图片压缩,仔细看配置说明,发现是编辑器自带有个自动压缩图片的功能,可恶的是,压缩后的图片失真了!
决定去掉这个压缩功能,如果非得要压缩,那就交给服务端PHP来处理吧!
找到配置文件:ueditor/php/config.json
把 imageCompressEnable 设置为 false 即可!
【总结】有2个比较重要的配置文件:
ueditor/ueditor.config.js
ueditor/php/config.json
代码如下 | 复制代码 |
# 查找
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); |
解决 办法一,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服务,问题解决