Category Archives: PHP Scripts

It contains PHP Scripts that I learn newly.

Regular Expression for Email Validation

The regular expression for email validation is follows :

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$

The JS script for email validation can be written as follows :

function email_validator(mail_id) {
    var regexp=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/;
    if (regexp.test(mail_id)) {
        alert("This is a valid Email Id!!!");
        return true;
    }
    else {
        alert("This is an invalid Email Id!!!");
        return false;
    }
}

How to create a folder in PHP?

In PHP we can create a folder using mkdir() function.

eg:

>?php
       mkdir("testing");
?>

How to check availability of a folder in PHP?

We can check the availability of a folder using PHP as follows :

if(file_exists ( "folder_name" ))
{
     echo "Folder Exists";
}
else
{
     echo "Folder Not Exists";
}

Script to download a file in PHP

To make a file to download in php, we can use the following code :

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
ob_clean();
flush();
readfile($file_name);

Linux System Command to Zip Files

The linux system command to create zip single or multiple files are as follows :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext

If the files are in another folder and you see those file names if the downloaded zip file in which you wish not to see these files, then use the following command :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext