Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

PHP Recursive dir size with iterator object

public function getDirSize($dir, $format = true)
    {
        $dir_iterator = new RecursiveDirectoryIterator($dir);
        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
        $size = 0;
        foreach ($iterator as $file) {
            $size += $file->getSize();
        }

        if ($format) {
            if ($size / 1048576 > 1) {
                return round($size / 1048576, 1) . ' MB';

                // if the total size is bigger than 1 KB
            } elseif ($size / 1024 > 1) {
                return round($size / 1024, 1) . ' KB';

                // else return the filesize in bytes
            } else {
                return round($size, 1) . ' bytes';
            }
        } else {
            // return the total filesize in bytes
            return $size;
        }
    }

usage:


$size = getDirSize('/dir/to/get/size/');

function get_currency_rate

Simple, yet effective way to get currency rate from yahoo finance service. Cache the results, or you will get ban from yahoo.

How to check if image has no code injected?

If you let images with code injection be saved on your server, it is possible, that it's a highwayto your php files and whole database, so it is important to make sure, that if user uploads avatar, it is really an avatar, not some hacky stuff with code injected into the image.

Symfony 2 and ZipArchive - class not found!

It was crazy. I was trying to make my ZipArchive work and I know now, that it was already working just Symfony2 did not knew about it!

Fatal error: Class 'Demo\AcmeBundle\Controller\ZipArchive' not found 





php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory while installing php zip

I was trying to use ZipArchive, but i got an error:
Fatal error: Class 'ZipArchive' not found
Then I started to Google how to install zip extension on my Ubuntu Apache 2 server. I have PHP version 5.3.10 and in configuration it says, that zip is enabled. This information you can get with these 2 simple commands:

How to use template engine in Symfony 2?

Symfony 2 comes bundled with Twig template engine, so you definitely want to use it. Twig template engine has everything what you need to keep your business logic out of templates and stay on MVC pattern, which is used in Symfony 2. The best thing about Twig template engine is that in many cases it is even faster than raw PHP, because finally it renders to raw and optimised PHP code.







How to join 2 SQL tables with PHP? Examples and visualisations


Often you need to join data from 2 tables. You can do it with several SQL queries and PHP, or just use JOIN functions. The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Most popular joins are INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN. It's always good to know the different.








What is the different between HTML CSS PHP and JavaScript?

What is the different between HTML CSS PHP and JavaScript?
That's a good question. It's very important to understand this different before starting to develop any website. So let's dive in:

HTML - it's like bones for your site. It says what to display on the user's browser: Tables, titles, paragraphs, lists, links etc. It is client-side language which is understood by browsers like Google Chrome, FireFox, Safari.





How to make PHP unrar and unzip recursively folder with files?

Simple task - unzip zip archive and unrar rar archive. With zip it's all easy, just some basic PHP default ZipArchive class and you have it, but with rar - why to make things more complicated?