使用DeDe CMS v5.5的网站在后台上传图片时出现错误,作为web生手的我很是迷茫。印象里之前并没有做什么改动,于是百思不得其解,Google、百度、DeDe官网搜索了一大圈,发现有这个问题的人不少,但是没有一个给出解决的办法,只好自己试着分析分析……
找到提示出错的相关js文件:/dede/handlers.js
发现有这么一段:
- function uploadSuccess(file, serverData) {
- try {
- var progress = new FileProgress(file, this.customSettings.upload_target);
- if (serverData.substring(0, 7) === "FILEID:") {
- addImage("swfupload.php?dopost=thumbnail&id=" + serverData.substring(7), serverData.substring(7));
- progress.setStatus("获取缩略图...");
- progress.toggleCancel(false);
- } else {
- addImage("img/error.gif", 0);
- progress.setStatus("有错误!");
- progress.toggleCancel(false);
- alert(serverData);
- }
- } catch (ex) {
- this.debug(ex);
- }
- }
其中:if (serverData.substring(0, 7) === "FILEID:") 这句的判断为假导致如上图的问题.
查看serverData的值没有发现什么异常,但是判断始终为假,问题就出在serverData.substring(0, 7) 始终都不等于"FILEID:",调整了截取边界也一样有错,于是乎,请来了HttpAnalyzer一看,哦,原来如此.
虽然这么看着没什么问题,但实际问题在这,前面多了三个字节:ef bb bf,大致一找原来这是utf-8的bom问题,是修改文件时在文件头自动加入的这三个字节导致的连锁反应.
但是那么多文件要找出是哪个文件的问题倒是有点头大,后来找到一篇文章《转两篇 移除PHP中BOM的方法》按照其中给出的清除BOM的代码解决了这个问题,代码如下:
- <?php
- //remove the utf-8 boms
- //by magicbug at gmail dot com
- if (isset($_GET['dir'])){ //config the basedir
- $basedir=$_GET['dir'];
- }else{
- $basedir = '.';
- }
- $auto = 1;
- checkdir($basedir);
- function checkdir($basedir){
- if ($dh = opendir($basedir)) {
- while (($file = readdir($dh)) !== false) {
- if ($file != '.' && $file != '..'){
- if (!is_dir($basedir."/".$file)) {
- echo "filename: $basedir/$file ";
- echo checkBOM("$basedir/$file")." <br>";
- }else{ //开源软件:Vevb.com
- $dirname = $basedir."/".$file;
- checkdir($dirname);
- }
- }
- }
- closedir($dh);
- }
- }
- function checkBOM ($filename) {
- global $auto;
- $contents = file_get_contents($filename);
- $charset[1] = substr($contents, 0, 1);
- $charset[2] = substr($contents, 1, 1);
- $charset[3] = substr($contents, 2, 1);
- if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
- if ($auto == 1) {
- $rest = substr($contents, 3);
- rewrite ($filename, $rest);
- return ("<font color=red>BOM found, automatically removed.</font>");
- } else {
- return ("<font color=red>BOM found.</font>");
- }
- }
- else return ("BOM Not Found.");
- }
- function rewrite ($filename, $data) {
- $filenum = fopen($filename, "w");
- flock($filenum, LOCK_EX);
- fwrite($filenum, $data);
- fclose($filenum);
- }
- ?>
保存为a.php,放在网站根目录访问下就可以了.
如果在访问a.php的时候报错如:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
那就试用下面的修改版检查出有bom的文件自己修改吧.
- <?php
- //remove the utf-8 boms
- //by magicbug at gmail dot com
- if (isset($_GET['dir'])){ //config the basedir
- $basedir=$_GET['dir'];
- }else{
- $basedir = '.';
- }
- $auto = 1;
- checkdir($basedir);
- echo ("<br><br><font color=green>completed!</font><br>");
- function checkdir($basedir)
- {
- if ($dh = opendir($basedir))
- {
- while (($file = readdir($dh)) !== false)
- {
- if ($file != '.' && $file != '..')
- {
- if (!is_dir($basedir."/".$file))
- {
- //echo "filename: $basedir/$file ";
- checkBOM("$basedir/$file");
- }
- else
- {
- $dirname = $basedir."/".$file;
- checkdir($dirname);
- }
- }
- }
- closedir($dh);
- }
- }
- function checkBOM ($filename) {
- global $auto;
- $contents = file_get_contents($filename,NULL,NULL,0,10);
- $charset[1] = substr($contents, 0, 1);
- $charset[2] = substr($contents, 1, 1);
- $charset[3] = substr($contents, 2, 1);
- if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
- if ($auto == 1) {
- //$rest = substr($contents, 3);
- //rewrite ($filename, $rest);
- echo ($filename."--------"."<font color=red>BOM found</font><br>");
- } else {
- //return ("<font color=red>BOM found.</font>");
- }
- }
- //else return ("BOM Not Found.");
- }
- function rewrite ($filename, $data) {
- $filenum = fopen($filename, "w");
- flock($filenum, LOCK_EX);
- fwrite($filenum, $data);
- fclose($filenum);
- }
- ?>
新闻热点
疑难解答