How to fix undefined function utf8_decode error in PHP application

This is just another interesting problem to solve. I have encountered this subtle difference in PHP packages when I moved personal Dokuwiki instance from Debian Jessie to Debian Stretch.

Operating system version.

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.3 (stretch)
Release:	9.3
Codename:	stretch

HTTP error code 500 logged by nginx.

$ tail /var/log/nginx/access.log
[...]
10.250.3.36 - - [11/Jan/2018:14:52:46 +0100] "GET /doku.php?id=start&do=edit HTTP/1.1" 500 5 "http://10.250.4.250/doku.php?id=start" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:57.0) Gecko/20100101 Firefox/57.0"
10.250.3.36 - - [11/Jan/2018:14:52:49 +0100] "GET /doku.php?id=start&do=edit HTTP/1.1" 500 5 "http://10.250.4.250/doku.php?id=start" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:57.0) Gecko/20100101 Firefox/57.0"
10.250.3.36 - - [11/Jan/2018:14:53:50 +0100] "GET /doku.php?id=start&do=edit HTTP/1.1" 500 5 "http://10.250.4.250/doku.php?id=start" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:57.0) Gecko/20100101 Firefox/57.0"

Detailed strack trace by uwsgi pointing to the undefined function utf8_decode().

$ tail /var/log/uwsgi/app/php.log
[pid: 18443|app: -1|req: -1/1] 10.250.3.36 () {44 vars in 977 bytes} [Thu Jan 11 14:52:46 2018] GET /doku.php?id=start&do=edit => generated 0 bytes in 91 msecs (HTTP/1.1 500) 4 headers in 188 bytes (0 switches on core 0)
Thu Jan 11 14:52:49 2018 - PHP Fatal error:  Uncaught Error: Call to undefined function utf8_decode() in /srv/dokuwiki/inc/utf8.php:152
Stack trace:
#0 /srv/dokuwiki/inc/utf8.php(402): utf8_strlen('start')
#1 /srv/dokuwiki/inc/common.php(1128): utf8_ucfirst('start')
#2 /srv/dokuwiki/inc/common.php(1073): parsePageTemplate(Array)
#3 /srv/dokuwiki/inc/actions.php(574): pageTemplate('start')
#4 /srv/dokuwiki/inc/actions.php(156): act_edit('edit')
#5 /srv/dokuwiki/doku.php(124): act_dispatch()
#6 {main}
  thrown in /srv/dokuwiki/inc/utf8.php on line 152

Install php7.0-xml package to solve this issue.

$ sudo apt-get install php-xml
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  php7.0-xml
The following NEW packages will be installed:
  php-xml php7.0-xml
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 117 kB of archives.
After this operation, 496 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://ftp.task.gda.pl/debian stretch/main amd64 php7.0-xml amd64 7.0.19-1 [112 kB]
Get:2 http://ftp.task.gda.pl/debian stretch/main amd64 php-xml all 1:7.0+49 [5,068 B]
Fetched 117 kB in 0s (417 kB/s)
Selecting previously unselected package php7.0-xml.
(Reading database ... 29079 files and directories currently installed.)
Preparing to unpack .../php7.0-xml_7.0.19-1_amd64.deb ...
Unpacking php7.0-xml (7.0.19-1) ...
Selecting previously unselected package php-xml.
Preparing to unpack .../php-xml_1%3a7.0+49_all.deb ...
Unpacking php-xml (1:7.0+49) ...
Setting up php7.0-xml (7.0.19-1) ...

Creating config file /etc/php/7.0/mods-available/dom.ini with new version

Creating config file /etc/php/7.0/mods-available/simplexml.ini with new version

Creating config file /etc/php/7.0/mods-available/wddx.ini with new version

Creating config file /etc/php/7.0/mods-available/xml.ini with new version

Creating config file /etc/php/7.0/mods-available/xmlreader.ini with new version

Creating config file /etc/php/7.0/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/7.0/mods-available/xsl.ini with new version
Setting up php-xml (1:7.0+49) ...

Restart uwsgi service.

$ sudo /etc/init.d/uwsgi restart
[ ok ] Restarting uwsgi (via systemctl): uwsgi.service.

This happens because utf8_decode() function is now provided by php7.0-xml package in Debian Stretch, but it was provided by libphp5-embed package in Debian Jessie.

Debian Stretch library that contains above-mentioned function:

stretch$ readelf -s /usr/lib/php/20151012/xml.so  | grep utf8_decode
    75: 0000000000006e20   142 FUNC    GLOBAL DEFAULT   11 zif_utf8_decode
    98: 0000000000006150   482 FUNC    GLOBAL DEFAULT   11 xml_utf8_decode

Debian Jessie library that contains above-mentioned function:

jessie$ readelf -s /usr/lib/libphp5.so | grep utf8_decode
  1348: 0000000000351a30   343 FUNC    GLOBAL DEFAULT   11 xml_utf8_decode
  1598: 0000000000352630   146 FUNC    GLOBAL DEFAULT   11 zif_utf8_decode
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.