UTF8 BOM problems in PHP and XML

Yep, it’s a hell if you’ve got the UTF8  BOM (byte order mark) at the beginning of your PHP or XML files. These files need to send their own headers before anything else. Because of the BOM’s location, which is the first byte of the file, headers can not be received by browsers and unintented errors might occur.

For PHP the error will be “Warning: Cannot modify header information”, and for XML, “XML declaration allowed only at the start of the document”. If you are having header errors in your WordPress (including admin pages), it is most probably caused by a byte order mark in your theme files (First check : functions.php :) )

How you can find and delete the BOM from text files ? Most frameworks and editors include a setting for saving non BOM UTF8 files. Check your help file, or ask at the forum or helpdesk of the tool you are using.

Second, never use Notepad on Windows for development purposes. It directly inserts the BOM when you save your file in UTF8 format.

If you are dealing with hundreds of files, finding and deleting the BOM is time consuming. So here is a PHP file I wrote for finding the files that you’ll need to correct.

What this script does is actually check all files’ first bytes for byte order mark characters by recursively moving around your home folder and subfolders. Every subfolder and file is checked and reported. After the script ends, it will also give you a small list of files that have BOM…

You can also find this file at http://emrahgunduz.com/BOM.php.txt as this will be easier, I hope :) . Simply change the $HOME line to your home directory. If you are hosted on a Windows based machine, do not forget to change the $WIN line to 1, as Windows recursive find needs a different set of slashes.

Good luck on BOM. If you want to ask anything, please use comments.

PS. Delete the file after usage. This one prints the file list of your domain’s root and subfolders.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php 
 
//Tell me the root folder path.
//You can also try this one (not recommended for Windows hosts)
//$HOME = $_SERVER["DOCUMENT_ROOT"];
 
$HOME = "/home/somewhere";
 
// Is this server is a Windows OS Host ?
// If it is, change this line to $WIN = 1;
 
$WIN = 0;
 
//If it is not WINDOWS, keep it 0
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF8 BOM FINDER</title>
<style>
body {
	font-size: 10px;
	font-family: Arial, Helvetica, sans-serif;
	background: #FFF;
	color: #000;
}
.FILE {
	color: #0C0;
}
.DIR {
	color: #03C;
	font-style: italic;	
}
.BOM {
	color: #F30;
	font-weight: bold;
}
.FOUND {
	color: #F30;
	font-size: 14px;
	font-weight: bold;
}
</style>
 
</head>
 
<body>
 
 
<?php
CheckFilesForBOM($WIN); 
$BOMBED = array();
function CheckFilesForBOM($win32=0) {
	global $HOME, $BOMBED;
 
	$win32 = ($win32 == 1) ? "\\" : "/";
 
	$folder = dir($HOME);
 
	$foundfolders = array();
	while ($file = $folder->read()) {
		if($file != "." && $file != "..") {
			if(filetype($HOME . $win32 . $file) == "dir"){
				echo '<span class="DIR">'.$HOME . $win32 . $file." (DIR)</span><br />\n";
				$foundfolders[count($foundfolders)] = $HOME . $win32 . $file;
			} else {
				$BOM = SearchBOM(file_get_contents($HOME . $win32 . $file));
				if ($BOM) {
					$BOMBED[count($BOMBED)] = $HOME . $win32 . $file;
					echo '<span class="BOM">'.$HOME . $win32 . $file."</span><br />\n";
				} else {
					echo '<span class="FILE">'.$HOME . $win32 . $file."</span><br />\n";
				}
			}
		}
	}
	$folder->close();
 
	if(count($foundfolders) > 0) {
		foreach ($foundfolders as $folder) {
			RecursiveFolder($folder, $win32);
		}
	}
 
	//Ending...
	echo '<br /><br /><hr /><h2>These files have UTF8 BOM:</h2><br />'
		.'<p class="FOUND">';
	foreach ($BOMBED as $utf) {
		echo $utf ."<br />\n"; 
	}
	echo '</p>';
}
function RecursiveFolder($sHOME,$win32) {
	global $HOME, $BOMBED;
 
	$win32 = ($win32 == 1) ? "\\" : "/";
 
	$folder = dir($sHOME);
 
	$foundfolders = array();
	while ($file = $folder->read()) {
		if($file != "." && $file != "..") {
			if(filetype($sHOME . $win32 . $file) == "dir"){
				echo '<span class="DIR">'.$HOME . $win32 . $file." (DIR)</span><br />\n";
				$foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
			} else {
				$BOM = SearchBOM(file_get_contents($sHOME . $win32 . $file));
				if ($BOM) {
					$BOMBED[count($BOMBED)] = $HOME . $win32 . $file;
					echo '<span class="BOM">'.$HOME . $win32 . $file."</span><br />\n";
				} else {
					echo '<span class="FILE">'.$HOME . $win32 . $file."</span><br />\n";
				}
			}
		}
	}
	$folder->close();
 
	if(count($foundfolders) > 0) {
		foreach ($foundfolders as $folder) {
			RecursiveFolder($folder, $win32);
		}
	}
}
function SearchBOM($string) { 
    if(substr($string, 0,3) == pack("CCC",0xef,0xbb,0xbf)) { 
        return true;
    }
    return false; 
}
 
?>
 
</body>
</html>

About Adobe CS4 Dreamweaver (And Probably All Other Products) Installation Errors : Error Number 2

I needed Adobe Dreamweaver for developing our new web site for the programmer, as html and css. So I thought what can be better than Dreamweaver, as it’s the tool that I use for my freelance work.

As I won’t be needing Dreamweaver on this computer for more than a few day, I downloaded and tried to install the CS4 trial version.

I’m running on Windows 7 64bit Ultimate.

The installation started giving errors right before the Dreamweaver part. This is the error message:

Adobe Dreamweaver CS4 LangPack (en_US)
Error:
Error 2.

And if you try to run Dreamweaver, this message box appears:

Could not locate the Resources file in the Configuration folder. This file is
required to run Dreamweaver. Please reinstall the application.

For a few hours I tried to find how to solve this problem. Disabled all firewall, antivirus software, startup tools. A fresh restart, not opening any programs while restarting, even not moving the mouse, or praying. Nothing was helping.

At the end, a lost comment in the huge interwebs of blogs and forums saved my day while searching Google.

If you are using Vista or Windows 7 and encounter error 2, check this folder:

C:\Users\Public\Documents

I’m definitely certain that, you do not have that folder. Simply create it and watch the magic happen.

Isn’t it fun ? Wait! What’s more disturbing is that Adobe installation does not create or put any files or folders in here. Sooo strange :)

3dsmax : Name An Object From Material Name

This question came a few days ago via a comment. Is it possible to name an object by simply checking the material, and assign that name to the object. Answer is yes, and here is a script that will accomplish this easy text, plus some maxscript explanations :

A selected object’s material name can be accessed with :
$.material.name

Assigning a name to an object goes like this :
$.name = "MyNewName"

And for a unique name you can use this one :
$.name = uniqueName "MyNewName"

If you’ve got more than one object selected, you can do something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sel = selection as array
count = 1
undo on
  (
    if sel.count > 0 then
    (
      while count <= sel.count do
      (
        select sel[count]
        count += 1
        $.name = uniqueName ($.material.name as String)
      )
    )
)

This will get the material name, and assign it to the object. Plus, it adds an auto incremented number at the end of each name. You can simply delete “uniqueName”, if you do not want this number. Objects can have same names, 3dsmax will not mind.

There is no error caching in this script. If you select an object that is not a member of the mesh class (like lights, cameras, helpers, etc), you might encounter some errors.

Strange CSS Bug : 3 Line CSS Code Crashing Dreamweaver CS4

A friend of mine found a strange bug in Dreamweaver CS4. Who would guess that a 3 line CSS code would be able to totally crash Adobe Dreamweaver CS4?

Here is the code he tried to write :

1
2
3
@media print{
     #tepe, #rightpanel,
}

While i was trying to replicate this bug, Dreamweaver first crashed at #rightpanel. On my second attempt, i succeeded but was not able to save the file, as Dreamweaver died again. Third time, the same. Even saving this simple code in Notepad, and trying to open it with Dreamweaver creates a disaster.

My operating system is Windows 7 Ultimate (64bit). But i was also able to crash the software in the virtual Windows XP environment. I don’t think that this is a language specific bug, as my language is set to English, and my friend’s Turkish.

As CSS is not really complicated, and not a programming language like C# or PHP, there must not be any reserved variables or values. I simply don’t know why Adobe hates this simple 3 line code :)

Bug reported this and waiting for an answer. If I get one, I’ll post it here.

Edit : A mail came after a few hours of my report. Adobe was able to reproduce the bug and it is internally filed. They will look into it.

If you encounter this kind of error, be sure to use valid CSS. As this bug appears while writing the code you won’t be able to finish what you started, but however, opening the saved “error creating CSS file” and finishing it like this,

1
2
3
@media print{
     #tepe, #rightpanel {}
}

stopped the crashes.

So, only one tip comes to mind : Save as much as you can… continuously…

Windows 7 “God Mode”

Yes, it seems like Microsoft decided to include a secret god mode for Windows users. The new icon is actually the control panel with a twist. As you know, control panel screens include some actions that are listed on the left side of the window. With this new window, every available control panel action will be listed as icons. This is not something innovative, but for those (like me) who hate to search every/any action in colorful but not very useful control panel screens, this is something fast and fun.

To access this property, create a new folder on your desktop, and rename it to:

GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}

Now double click and check it out :)