Convert Bytes To KB, MB, GB in PHP

Say you were displaying the size of a file in PHP. You obviously get the file size in Bytes by using filesize(). You won’t have any idea what the file size is if you read it in Bytes. Bytes is useful for file transmission in a network, but not for human usage. So, it’s better to convert it to human readable form.The circled areas show the converted size from Bytes.

[READ MORE]

Append Contents to Files in Bash

In Bash, we can add contents to file easily using the > character. Like this : echo "myContents" > ~/myfile.txt What if you want to append contents to the file without overwriting ? The easiest way to do this is change the “>” to “>>”. Just by making this small change, appending will be done : echo "myContents" >> ~/myfile.txt Even if the file doesn’t exist, Bash will create one with the contents given to append.

[READ MORE]

Creating a File with Contents in Bash

It’s really easy to create a file in Bash with cat and > : cat "/home/simsu/file.txt" > "/home/simsu/file.txt" But, what if you want to add contents to file too ? In this case, we can use cat and echo. Here’s how we do it in echo : echo "My File, My Choice" > "/home/simsu/file.txt" But, there’s a problem with doing like this. Since there is an option to limit the characters of a Terminal command, adding large contents is not possible.

[READ MORE]

Accessing /var/log Files in PHP

If you have tried to access a log file in /var/log using PHP in Linux, you will see the “Permission denied” error. This is because, Apache doesn’t have read permission on the log file. Let’s look at the user groups that have permission to access the log files : it’s root and the program which created the file. Suppose, let’s say the log file is “/var/log/squid/access.log”. That file’s group and owner will be “proxy” and others won’t even have the read permission on it except root ofcourse.

[READ MORE]

Finding Exact Document Root in PHP

You might know the DOCUMENT_ROOT value of the $_SERVER array. This value indicates the absolute location of the site directory where pages are served. In short, it’s the public folder of site as defined in the server’s configuration. Here’s a quick example : If you have a site in /home/me/site and can access it by http://site.dev, then the $_SERVER[‘DOCUMENT_ROOT’] is “/home/me/site”. If any page in the server, even if it is in many sub folders, the DOCUMENT_ROOT value will be the same.

[READ MORE]

Find MIME Type of File in PHP

MIME type is needed to know what kind of file we’re looking at. HTML files have the MIME type text/html , for gif image, it’s image/gif and so on. Since PHP is a dynamic content creator language, MIME type is a very important thing you should know about in PHP. In this small post, I’ll tell you how to find the MIME type of a local file in your server.

[READ MORE]

How To Download & Extract Zip Archives in PHP

If you used WordPress before, you know that when you do plugin installation action, WordPress automatically downloads and extract the plugin zip file. If you’re app needs something like this, you will find this post useful. PHP has the Zip Archive Extension. You can see the php manual for installing ZipArchive Extension. The usage of ZipArchive extension is very easy. Download For downloading, we will use cURL. First we create a blank zip file, download the zip file from server and put it’s contents to the blank zip file we created.

[READ MORE]

Get Absolute Directory Path of Current File in PHP

If your code is including other files, you will encounter problems like “The file is not found”. This is because, PHP doesn’t know the absolute path of file. So, if you are including files on another file, you should mention the absolute path of file. But, If you’re PHP application is being used by other people, then the absolute path which you mentioned in your system won’t be the one of the others’ systems.

[READ MORE]

Uploading An Image Using AJAX In jQuery With PHP

As you know AJAX is the method of sending data without refreshing the page. Now I’m going to tell you how to upload images using AJAX in jQuery. For This You need to Download the jquery.form plugin from here. The Minimum requirement for this plugin is jQuery version 1.5 or later. Download Demo A Sample of uploading image using AJAX in jQuery is shown below :

[READ MORE]