Create and download ZIP file in PHP

Here I am sharing a script for creating and download the ZIP file in PHP. Sometimes we need to create a zip file and download that file that script will helpful for that case.

Look at the above snippet, here you can see a function called createZipAndDownload() which takes $files,  $filesName, and  $zipFileName as a parameter. To use this pretty function, first of all, create an array with the file name. And then pass that files array as the first argument of createZipAndDownload(). The second argument with being directory path of these files and the third argument will be the name of zipping file.

Example:

// Files which need to be added into zip
$files = array('sample.php', 'sample.jpg', 'sample.pdf', 'sample.doc');
// Directory of files
$filesPath = '/ROOT/FILE_PATH';
// Name of creating zip file
$zipName = 'document.zip';
echo createZipAndDownload($files, $filesPath, $zipName);

 

 

Concurrency Issues With PHP Session

Concurrent connection means how many request sending/calling at the same time. It influences the application performances rapidly. When your application can handle as much as concurrent connections your application will become more stable, high scalable and faster as well. But concurrency might be a problem when you deal with PHP Session. It is the biggest challenges to avoiding concurrency issue in PHP, especially in PHP Session.

Most of the modern web browser sent concurrent connections to a specific domain to between 4 or 6. That means if your application has a lot of static assets (JS, CSS, Images, Fonts, etc), then these will be queued until browser get the response from the server for its first set of a request (4/6 concurrent request).

Concurrency Issues With PHP Session

PROBLEM:

By default, PHP uses files to storing the Session data. PHP either create a new Session or retrieve existing Session on each request when called session_start() function. So in this case, if your user sending multiple concurrent requests to the server which involves with Session data. PHP Session will lock and block your application’s concurrent request for that particular client. And every request will be served as sequentially instead of processing them concurrently.

BEHIND THE SENCE:

For the new user, PHP sent the unique identifier number to users computer and create a new file with the same unique identifier name for handle the session for that particular user. Otherwise, PHP retrieves existing session for that user. See this article to know how does session works.

Now assume, a user sent 6 concurrent requests to server and 4 of them involved with session data. So what happened that case? PHP will take the first request and open the session file and lock it. And other 3 requests will be queued until session file will be released. So that means your application will be blocked. After your first request’s done, it will release the session file and the second request will take it. It’s will happen for rest of the request.

Now think every request taken 400-500 ms to execute, that means your last (4th) request completed after 1600 ms cause it waited 1200 ms to complete first 3 requests. So in this way PHP Session blocked the application.

However, this concurrent issue happened for the same user only. Request from a user cannot block another user’s request.

You will be able to understand how this thing can affect your application performance. To improve application performance every developer needs to avoid this issue.

Terminate PHP Loop After a Certain Time

We all know every loop has a termination condition (conditions can be anything). And this code snippet is for terminating a while loop after a certain time. This is an actually funny script which is, increasing a value a certain time and then decreasing that value a certain time period.

From the above code snippet, you can see there is variable called $totalTime which carrying the total number of execution time (in second) for loop. Then $totalTime is divided into the two periods, and the first half is using to increasing the $counter value and the second half is using to decreasing that $counter value.

Output:

Terminating a PHP loop after a certain time preiod Raw

 

How Does PHP Session Works

Basically, the session is the system or way to store information for individual users. It’s storing individual users information against a unique session ID. So in this way, user’s data can be accessible across all pages of a website. PHP follow simple workflow for manage session, and that is when a session is started, then PHP either retrieve existing session or create a new session. If PHPSESSID is passed then PHP retrieved existing session otherwise creates a new session

By default, session creates a file in a temporary directory (which determined in php.ini file) on a server. All all the variables & value are stored there.

How Does PHP Session Works

How is Works:

  • Firstly PHP creates a unique identifier number (a random string of 32 hexadecimal number, e.g 3c7foj34c3jj973hjkop2fc937e3443) for an individual session.
  • PHPSESSID cookie passes that unique identification number to users’ browser to save that number.
  • A new file is created on the server with the same name of unique identification number with sess_ prefix (ie sess_3c7foj34c3jj973hjkop2fc937e3443.)
  • The browser sends that cookie to the server with each request.
  • If PHP gets that unique identification number from PHPSESSID cookie (on each request), then PHP searches in the temporary directory and compares that number to the file name. If both are the same, then it retrieves the existing session, otherwise it creates a new session for that user.

A session gets destroyed when the user closes the browser or leaves the site. The server also terminates the session after the predetermined period of session time expires. These are the simple mechanism steps that PHP is using to handle the session. I hope this article with help you to understand how PHP SESSION is working.

Return Image as response by PHP

Return Image as response By PHP

To return an image as a request response by PHP you fpassthru function of PHP. I have included a sample code snippet, which will help you to understand how to do this.

From the above code snippet, you can see I have opened an image with binary mode by fopen($image, rb). And then send that with appropriate headers by header(“Content-Type: image/png”). After that dumping that image by using fpassthru, and finally stop the execution of PHP. It’s really easy to return image as response by PHP

What is Anonymous Function in PHP

Anonymous is a normal PHP function but there is no specified name. Anonymous function seems like a normal/regular PHP function, it’s has a code block and run when it called, return value, it’s takes arguments as well. The major different between regular PHP function and Anonymous function are:

Anonymous function doesn’t need any name to declare the function but regular PHP function must need a name to declare.

Anonymous Function in PHP
Anonymous Function in PHP

From this image, you can see the  Anonymous function has no name between function keyword and.

And another major different is Anonymous function has a semicolon (;) after the function definition (see the image above). These are very major different between regular and Anonymous function.

 

In the a above image’s anonymous function is valid but not callable yet from anywhere. So it isn’t useful, To create a callable anonymous function we need to do a little more. I will show that rest of the article. So don’t worry

 

The Anonymous function is extremely useful as a value of a callback function. It can be really useful in certain situations as well. I will explain to you how to create an Anonymous function, how to use it and how to use it as a value of callback function.

To create an Anonymous function and access it, you can do a couple of handy things with it. For example,

  • Create an anonymous function and assign it to a variable, then call that anonymous function by using that assigned variable.
  • Create an anonymous function and pass it as an argument to another function. (callback function)

 

Creating Anonymous function and assign into variable:

Create an anonymous function and store that into a variable, Something like that

Here you can see, I have created a function and storing that into $anonymousVariable variable and calling. Now we can call access this anonymous function by using this $anonymousVariable variable. I am calling the anonymous function by this way echo $anonymousVariable('World'); here ‘World’ is the argument of the anonymous function.    

 

From this above code snippet, you will get this output.

PHP Anonymous function
PHP Anonymous function

 

 Create Anonymous function and pass it as an argument to another function:

One of the useful use of Anonymous is, build a callback function. A callback is a function (you will create) and passes to another function as an argument. Some PHP built is function allow add custom functions (e.g array_map() ). I will show this example with using array_map()  function. See the code snippet below.  

 

From this above code snippet, you will get this output.

Output of Anonymous function

So, now you explored about what is Anonymous function and some basic usage of it. Hope this article will help you to understand the about Anonymous function.