PHP 5.4 updates I love most of all. Like php file uploads & mysql query result

Sure there a re lot of cool improvements (you can read about incompatibilities & new features ), but some of them much more important for me than other. I mus notice that PHP is growing with each release, becoming more mature object oriented platform and incorporates more and more features that are making web programming efficient, simple and fast.

  • Short opening tags - probably the main feature of the language!
  • Short array syntax - oh! I love it so much!
  • Binary number format
  • File upload progress - how did we survive without it till now?
  • MySQL extension improvement - really cool one, read below.

I am not going to describe all of the features, but just want to mention two that are most important to my mind. So, file upload progress that I missed here for so long!

Here are the changes that should be applied to php.ini:

; Enable upload progress tracking in $_SESSION
; Default Value: On
; Development Value: On
; Production Value: On
; http://php.net/session.upload-progress.enabled
session.upload_progress.enabled = On

; Cleanup the progress information as soon as all POST data has been read
; (i.e. upload completed).
; Default Value: On
; Development Value: On
; Production Value: On
; http://php.net/session.upload-progress.cleanup
session.upload_progress.cleanup = On

; A prefix used for the upload progress key in $_SESSION
; Default Value: "upload_progress_"
; Development Value: "upload_progress_"
; Production Value: "upload_progress_"
; http://php.net/session.upload-progress.prefix
session.upload_progress.prefix = "upload_progress_"

; The index name (concatenated with the prefix) in $_SESSION
; containing the upload progress information
; Default Value: "PHP_SESSION_UPLOAD_PROGRESS"
; Development Value: "PHP_SESSION_UPLOAD_PROGRESS"
; Production Value: "PHP_SESSION_UPLOAD_PROGRESS"
; http://php.net/session.upload-progress.name
session.upload_progress.name = "PHP_SESSION_UPLOAD_PROGRESS"

; How frequently the upload progress should be updated.
; Given either in percentages (per-file), or in bytes
; Default Value: "1%"
; Development Value: "1%"
; Production Value: "1%"
; http://php.net/session.upload-progress.freq
session.upload_progress.freq =  "1%"

; The minimum delay between updates, in seconds
; Default Value: 1
; Development Value: 1
; Production Value: 1
; http://php.net/session.upload-progress.min-freq
session.upload_progress.min_freq = "1"

index.php

<? session_start(); ?>
<html>
    <head>

    </head>
    <body>

        <form action="upload.php" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
            <input type="file" name="file1" />
            <input type="file" name="file3" />
            <input type="file" name="file2" />
            <input type="file" name="file4" />
            <input type="submit" />
        </form>
    </body>
</html>

progress.php

<?php
session_start();
?>
<html>
    <head>
        <meta http-equiv="refresh" content="1">

    </head>
    <body>
        <?
        if (isset($_SESSION["upload_progress_123"])) {
            print_r($_SESSION["upload_progress_123"]);
        }
        ?>
    </body>
</html>

Output example shows the structure of the progress info populated to the session. It is very simple and usable:

Array (
    [start_time] => 1324664225
    [content_length] => 444446321
    [bytes_processed] => 398904160
    [done] =>
    [files] => Array (
        [0] => Array (
            [field_name] => file1
            [name] => xxx.txt
            [tmp_name] => /tmp/phpVJK396
            [error] => 0
            [done] => 1
            [start_time] => 1324664225
            [bytes_processed] => 111111387 )
        [1] => Array (
            [field_name] => file3
            [name] => xxx.txt
            [tmp_name] => /tmp/phpbkWd62
            [error] => 0
            [done] => 1
            [start_time] => 1324664227
            [bytes_processed] => 111111387 )
        [2] => Array ( [field_name] => file2
            [name] => xxx.sql
            [tmp_name] => /tmp/phpYVWD55
            [error] => 0
            [done] => 1
            [start_time] => 1324664229
            [bytes_processed] => 111111387 )
        [3] => Array (
            [field_name] => file4
            [name] => xxx.sql
            [tmp_name] =>
            [error] => 0
            [done] =>
            [start_time] => 1324664232
            [bytes_processed] => 65569271 ) ) )

It is now very simple to build ajax progress controls without different tricks like perl or other scripts, flash or java uploaders. Just PHP & Javascript....

One more improvement that I would like to pay attention to is enhanced MySQL extension.  Now it allows to get the result of the query execution and instantly iterate through the results of the query, as now result contains the data:

$mysqli = new mysqli('localhost', 'root', 'xxxx', 'cars');

$result = $mysqli->query("SELECT * FROM manufacturers LIMIT 10");

foreach($result as $manufacturer){
    echo $manufacturer['name'];
}
$mysqli->close();

Sorry that I described everything so brifely, as the chenages that are going are really very important and promising. Developers are moving in a correct direction and I really would like to thank them for their great job.

Follow my blog, as I am probably will try to use the benefits of that improvements and most likely will post some code here.

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)