Category Archives: Php Snippets

Php code snippets and examples

Php – Fix “Input is not proper UTF-8, indicate encoding” error when loading xml

By | March 26, 2013

When loading xml files in php through simplexml_load_string or domDocument class, sometimes an error like this might popup Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding ! OR Warning: simplexml_load_string(): Entity: line 93: parser error : Input is not proper UTF-8, indicate encoding ! The error occurs when the xml has some invalid characters… Read More »

Php – Fetch gzipped content over http with file_get_contents

By | March 24, 2013

The file_get_contents function is often used to quickly fetch a http url or resource. Usage is very simple and appears like this $content = file_get_contents(‘’); However the file_get_contents does not get the contents compressed. It requests the server to send everything in plain text format. Most websites are capable of serving compressed content, if they… Read More »

How to download a file using Curl in PHP – Code Snippet

By | April 23, 2019

Download a File using Curl Here is a quick curl snippet for php, that can download a remote file and save it. <?php set_time_limit(0); // File to save the contents to $fp = fopen (‘files2.tar’, ‘w+’); $url = "http://localhost/files.tar"; // Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url));… Read More »

Get the path of tmpfile in php

By | March 14, 2013

tmpfile is a very handy function to create temporary files. It returns the file handle/resource. If the path of the file is needed, for use with file_put_contents for example then here is a quick trick to do that $path = array_search(‘uri’, @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile()))); file_put_contents($path, ‘hello’); echo file_get_contents($path); Last Updated On : 14th March 2013

How to compress images in php using gd

By | March 12, 2013

Php applications might need to do some image processing if they are allowing users to upload pictures of somekind. This can include cropping, watermarking, compressing etc. To compress an image the quality needs to be adjusted. Here are few examples function compress_image($src, $dest , $quality) { $info = getimagesize($src); if ($info[‘mime’] == ‘image/jpeg’) { $image… Read More »

Generate a dropdown list of timezones in php

By | February 14, 2018

Applications often allow users to select their timezones for reporting the proper time properly. Here is a quick function that can be used to generate a dropdown list of timezones that is easy to read and understand. &lt;?php function get_timezones() { $o = array(); $t_zones = timezone_identifiers_list(); foreach($t_zones as $a) { $t = ”; try… Read More »

Php array of iso 639-1 language codes and names

By | November 22, 2012

Here is a php array containing the language codes and language names as specified by the iso 639-1 standard. Useful when . &lt;?php /** ISO 639-1 Language Codes Useful in Locale analysis References : 1. 2. */ $language_codes = array( ‘en’ =&gt; ‘English’ , ‘aa’ =&gt; ‘Afar’ , ‘ab’ =&gt; ‘Abkhazian’ , ‘af’… Read More »