Signing problems with PHP UTF8 files

  • 2020-03-31 16:46:10
  • OfStack

That is, there is BOM format coding, or no BOM format coding.
If you look at the contents of the file, you will not see any difference, take the following file (schema.sqlet.sql) for example:
Schema. Sqlite. SQL
 
CREATE TABLE guestbook ( 
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
email VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com', 
comment TEXT NULL, 
created DATETIME NOT NULL 
); 
CREATE INDEX "id" ON "guestbook" ("id"); 

The size of the file is 232 bytes if there is no signature and 235 bytes if there is a signature.
The UTF8 signature has three bytes (content: EFBBBF) and is designed to tell the software that the file is UTF8 encoded.
In general, the presence or absence of a signature is not a problem because the editor or other software can infer whether it is UTF8 based on the content of the text.
But sometimes it can cause problems, like an appeal document. The file is an SQL statement file, and the program executes the SQL exactly through the following statement (PHP) :
 
$schemaSql = file_get_contents(dirname(__FILE__) . '/schema.sqlite.sql'); 
$dbAdapter->getConnection()->exec($schemaSql); 

In this case, files with signatures cause problems because "three bytes for UTF8 signatures" is actually at the top of the file. As a result, the above statement failed to run successfully.
The solution is also simple, remove the file UTF8 signature.
Of course, the contents of the above file are all single-byte, so there is no need to save them as UTF8.

Addition: files that are all single-byte content will remain the default encoding of the system until they are signed in UTF8.

Related articles: