Work with ZIP-archives in PHP 5.2+

Starting with PHP 5.2 you can use useful library ZipArchive for easy working with ZIP archives.
(If your PHP version les 5.2 you’ll get error: class ziparchive not found)

Steps for easy working with ZipArchive
Init:

$zip = new ZipArchive;

Read ZIP archive:

$zip->open('{file_location_path}');

Create new ZIP archive (just add ZipArchive::CREATE):

$zip->open('{file_location_path}', ZipArchive::CREATE);

Example of extracting archive on your server:

$zip = new ZipArchive;
if ($zip->open('{file_location_path}') === true){
	$zip->extractTo('{file_location_extraction_path}');
	$zip->close();
}else{
	echo 'Archive file is not found';
}

Example of creating archive on your server:
Continue reading Work with ZIP-archives in PHP 5.2+

What difference between extension, zend_extension, zend_extension_ts in PHP.ini (extension VS zend_extension)?

If you see in php.ini something like:

extension=php_pdo_sqlite.dll
zend_extension = php_xdebug-2.5.1-7.1-vc14.dll
;zend_extension_ts = php_xdebug-2.0.4.dll ;for PHP<=5.2

So,
extension – it’s native PHP extension
zend_extension – it’s Zend extension for PHP(mainly using for profiling/debug).
Both extension kinds share lots of stuff. The difference between both types is mainly in hooks they register into the Engine.
Remember that, despite it is very uncommon, an extension can be both a PHP extension and a Zend extension at the same time. Xdebug is a good example.

zend_extension_ts – it’s old(for PHP <5.3) threads safe Zend extension (not actual)
Detailed scheme of loading extension and zend_extension:


More detailed info you can read here: https://wiki.php.net/internals/extensions

Install Xdebug on Windows (PHP)

Simple steps for install XDebug on your Windows machine.
0. Write on test.php this code:

phpinfo();

1. Copy all informmation from phpinfo page.
2. Open https://xdebug.org/wizard.php and insert phpinfo to textarea
3. Click “Analyse my phpinfo() output” and follow to provided instructions
Something like:

  • Download php_xdebug-2.5.1-7.1-vc14.dll
  • Move the downloaded file to E:\xampp\php\ext
  • Update E:\xampp\php\php.ini and change the line zend_extension = E:\xampp\php\ext\php_xdebug-2.5.1-7.1-vc14.dll
  • Restart the webserver
$obj1 = new stdClass();
$obj1->name = 'A';
$obj1->date = date('Y-m-d H:i:s', 1458754362);
var_dump($result);

Happy debugging.

If you catch: php5-imap requires ext-imap * -> the requested PHP extension imap is missing from your system.

If you see: “The requested PHP extension imap is missing from your system.”
It seems that you need to install imap extension on your system.
The following simple steps below:

apt-get install php5-imap

However, it’s not enabled IMAP by default so enable it with:

php5enmod imap

Then bounce apache:

service apache2 restart

If you got “IMAP extension must be enabled.” in PHP.
Continue reading If you catch: php5-imap requires ext-imap * -> the requested PHP extension imap is missing from your system.

PHP. Save information into text/log file. Bacis examples.

Simple example for save information to file on server by PHP

$content = '[' . date('Y-m-d H:i:s') . ']: test info log' . PHP_EOL . '------------' . PHP_EOL;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/save_test.log", "a+");
fwrite($fp, $content);
fclose($fp);

Error log using example(logs $content [array|string|object] to the log file):

$obj = new StdClass();
$content = [$obj, [3, 51, '4k']];
error_log('[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . print_r($content, TRUE) . PHP_EOL, 3, $_SERVER['DOCUMENT_ROOT'] . "/save_test.log");

Also you can use file_put_contents method:
Continue reading PHP. Save information into text/log file. Bacis examples.

Native PHP Sort array of objects by property

Usually, it is very often necessary to sort the data in an array. But when in the array we have objects it happened little bit difficult.
Fast solution for sorting array objects by property name.
Bacis solution (PHP 5.3+) :

function sortArrayWithObjects($array, $property)
{
    usort($array, function ($a, $b) use ($property) {
        return (($a->$property == $b->$property) ? 0 : (($a->$property < $b->$property) ? -1 : 1));
        //[short version] return strcmp($a->$property, $b->$property);
    });
    return $array;
}

$obj1 = new stdClass();
$obj2 = clone $obj1;
$obj3 = clone $obj2;
$obj1->name = 'B';
$obj2->name = 'C';
$obj3->name = 'A';
$data = [$obj1, $obj2, $obj3];
$result = sortArrayWithObjects($data, 'name');
echo '<pre>';
var_dump($result);
echo '</pre>';

Result:
Continue reading Native PHP Sort array of objects by property

PHPExcel not working with PHP 7.+ – ZipArchive not found

PHPExcel ZipArchive not found

If you catch this error (and you use PHP 7 version) you need:
1. update PHPExcel to 1.8.+
or
2. try to use this preparing method:

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

P.s. https://github.com/PHPOffice/PHPExcel/issues/716

How to get timestamp of start to end (current) day period on PHP?

$dtNow = new \DateTime();
$beginOfDay = clone $dtNow;
$beginOfDay->modify('today');

$endOfDay = clone $beginOfDay;
$endOfDay->modify('tomorrow');
$endOfDay->modify('1 second ago');

$start_timestamp = $beginOfDay->getTimestamp();
$end_timestamp = $endOfDay->getTimestamp();

echo 'Start: '.$start_timestamp." [".date("Y-m-d H:i:s",$start_timestamp)."]"."\n";
echo 'End: '.$end_timestamp." [".date("Y-m-d H:i:s",$end_timestamp)."]"."\n";

Also, you can get first and last timestamps of a current month:
Continue reading How to get timestamp of start to end (current) day period on PHP?

Web-server on Ubuntu (PHP + MySQL + Apache) in 2 commands

Fast and easy install (PHP + MySQL + Apache) LAMP on Ubuntu

Step 1: install tasksel

sudo apt-get install tasksel

Step 2: Run install PHP, MySQL & Apache as LAMP server

sudo tasksel install lamp-server

Completed! Now you have installed PHP, MySQL, and Apache on your Ubuntu

Additional: Create site area
Continue reading Web-server on Ubuntu (PHP + MySQL + Apache) in 2 commands

If you got 500 (Internal Server Error) PHP/Nginx/Apache – Where you can found logs/reason?

Oh, noooo. You got most popular developer error 🙂
Firstly, No despair – say “Yes, I CAN”, and concentrate on possible errors.

1 step: Double check your PHP/Python/ETC code.
find_errors_ercule
Show all errors (how to show all errors/warnings/nootices in php)
Also, you can do “fast debug” in main parts of you code (Sure, use die() command but very carefully)

 

2 step: Check server/Apache&PHP settings
domino_server_settings_phpFirstly check .htaccess file on strange symbols or sentences.
Also, if you using POST method with uploading files then check php settings in php.ini (max_post_size, max_executing_time…)

 

3 step: Check error logs (Ubuntu/Linux)
find_errors_in_logs
Standard Apache logs dev finiding here:

/var/log/apache2/...

nginx logs here:

/var/log/nginx/...

Also, some popular frameworks logs folders:
Continue reading If you got 500 (Internal Server Error) PHP/Nginx/Apache – Where you can found logs/reason?

Fix Twilio SSL error. Services_Twilio_TinyHttpException SSL certificate problem

If you get on local yii site this error:
Services_Twilio_TinyHttpException
SSL certificate problem: self signed certificate in certificate chain

You can fix it in this file:
\vendor\twilio\sdk\Services\Twilio\TinyHttp.php
Just find and add this parameter CURLOPT_SSL_VERIFYPEER => FALSE for CURL connection:

$opts = $this->curlopts + array(
      CURLOPT_URL => $url,
      CURLOPT_HEADER => TRUE,
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_INFILESIZE => -1,
      CURLOPT_POSTFIELDS => NULL,
      CURLOPT_TIMEOUT => 60,
      CURLOPT_SSL_VERIFYPEER => FALSE
    );

Thats all. Happy SMSing…

PHP debug[timer] – Time of execute/load your code in the page

So, what about performance of your PHP code? 🙂 Answers is simple after tracking the script execution time of your code…
For get executing time of part of your code or of all page time loading you can use simple construction:

$start_time = microtime(true);
// your code here
$end_time = microtime(true);
$result_time = $end_time - $start_time;
echo "Debug info: Code loaded $result_time seconds\n";

Joomla error instalation of component issue

If you install some component on your site and get this errors:

Warning
JFolder::create: Infinite loop detected.
Warning: Failed to move file!

and

Error:
Archive does not exist
Unable to find install package

 

Then 97% error in your configuration file.
Please go to admin area and
Continue reading Joomla error instalation of component issue