Category Archives: Uncategorized

How to Enable Display of Errors in PHP

First edit your PHP.ini file to make sure display_errors is enabled: display_errors = ondisplay_errors = on Then if you changed this value, you’ll need to restart PHP7-FPM process: /etc/init.d/php7.3-fpm restart/etc/init.d/php7.3-fpm restart Additionally you may prepend these lines on your PHP script: error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);

Go One Directory Level Up/Back with Dirname() in PHP

For PHP < 5.3 use this: echo realpath(dirname(__FILE__) . '/..');echo realpath(dirname(__FILE__) . '/..'); For PHP 5.3 to 5.6 use this: echo realpath(__DIR__ . '/..');echo realpath(__DIR__ . '/..'); For PHP >= 7.0 use this: echo dirname(__DIR__, 1);echo dirname(__DIR__, 1); References:

Get Current Script Path in PHP

Snippet to get current script path in PHP: echo realpath(dirname(__FILE__));echo realpath(dirname(__FILE__)); It will output the current PHP script path, i.e: /var/www/

How to Extract a GZ file with PHP

A simple function to extract a .gz compressed file with PHP: function php_extract_gz_file($gzfile, $savefileas, $removegz = false) { if(!file_exists($gzfile)) return false;   $gzopen_handle = gzopen($gzfile, ‘rb’);   if(!$gzopen_handle) return false;   $outfile_handle = fopen($savefileas, ‘wb’);   if(!$outfile_handle) return false;   while(!gzeof($gzopen_handle)) { fwrite($outfile_handle, gzread($gzopen_handle, 4096)); }   fclose($outfile_handle);   gzclose($gzopen_handle);   if(!file_exists($savefileas)) return false;   […]

How to Install VirtualBox Guest Additions on Windows Guest

From your Windows VM open Internet Explorer and open: *** -> View all VirtualBox versions *** Then download the file: VBoxGuestAdditions_6.0.10.isoVBoxGuestAdditions_6.0.10.iso Then download and install 7-zip: Now right-click on VBoxGuestAdditions_6.0.10.iso and select: 7-Zip -> Extract Here7-Zip -> Extract Here This will extract the files from the ISO file. Now click on VBoxWindowsAdditions-amd64.exe […]

PHP Convert an Image File to Base64

PHP code snippet to convert an image file to base64: $base64_image = "data:image/png;base64,".base64_encode(file_get_contents("/path/to/image.png"));$base64_image = "data:image/png;base64,".base64_encode(file_get_contents("/path/to/image.png")); Need to convert a base64-encoded image back to file? Read this: PHP Convert Base64-Encoded Image to File

PHP Convert Base64-Encoded Image to File

Simple function to convert a base64-encoded image to an image file: function base64_to_image($base64_string, $output_file) { $file_handle = fopen($output_file, ‘wb’);   if(!$file_handle) return array("status" => "error", "message" => "Failed to create output file");   $image_types = array("data:image/png;base64,", "data:image/jpeg;base64,", "data:image/jpg;base64,", "data:image/bmp;base64,", "data:image/svg+xml;base64,", "data:image/x-icon;base64,");   $base64_string = str_replace($image_types, ”, $base64_string);   $base64_decoded = base64_decode($base64_string);   if(!$base64_decoded) return array("status" […]

PHP Correctly Get Last 12 Months (Month per Month)

PHP snippet to get the last 12 months, month per month: $dt = new DateTime(‘first day of this month’);   for ($i = 1; $i <= 12; $i++) { echo $dt->format(‘M Y’), "\n";   $dt->modify(‘-1 month’); }$dt = new DateTime(‘first day of this month’); for ($i = 1; $i <= 12; $i++) { echo $dt->format(‘M […]

PHP StrToTime() Returns Incorrect Timestamp ‘-1 MONTH’

If you use date() with strtotime() to get the last 12 months (or the last 1 month, the last 2 months, etc) you may have noticed that when you are on the last day of the current month, i.e 31 of May, you will get repeated months, for examples: echo date("M Y"); echo date("M Y", […]

PHP Multi-cURL to Run Parallel cURL Requests (Example)

Simple PHP function to run multiple cURL requests in parallel: function multicurl_urls($nodes) { $node_count = count($nodes);   $curl_arr = array(); $master = curl_multi_init();   for($i = 0; $i < $node_count; $i++) { $url =$nodes[$i]; $curl_arr[$i] = curl_init($url); curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true); curl_multi_add_handle($master, $curl_arr[$i]); }   do { curl_multi_exec($master,$running); } while($running > 0);   $results = array(); […]