8 Useful PHP Functions You Need to Know

If you are familiar with the basics of PHP, then you’re surely familiar with the built-in PHP functions. PHP is very rich in terms of built-in functions. It has more than 1000 built-in functions. These functions can increase efficiency and speed-up development if uses smartly.

Also, read:

Here are some useful PHP functions if they used properly and regularly, will surely make you smile!

is_writable

This function is really very useful to check whether a particular file is writable or not. This function returns TRUE if the file is exist and writeable. This function might be useful when we want to write something to a file and we wouldn’t know about permissions. So you can use to make sure about writable access.

$my_file = 'my_File.txt';
if (is_writable($my_file)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

is_uploaded_file

The function checks and tells whether the specified file is uploaded via HTTP POST. This function returns TRUE if the file is uploaded via HTTP POST. The function is useful to ensure that a malicious user hasn’t tried to trick the script into working on files upon which it should not be working.


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
   echo "Displaying contents\n";
   readfile($_FILES['userfile']['tmp_name']);
} else {
   echo "Possible file upload attack: ";
   echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
}

is_readable

This function checks whether the specified file is readable or not. It returns TRUE if the file or directory specified by filename exists and is readable otherwise it returns FALSE.

$filename = 'my_File.txt';
if (is_readable($filename)) {
    echo 'The file is readable';
} else {
    echo 'The file is not readable';
}

is_executable

You can use this function to check whether the specified file is executable or not. This function returns TRUE if the file is executable.

$file = '/home/vincent/somefile.sh';
if (is_executable($file)) {
echo $file.' is executable';
} else {
echo $file.' is not executable';
}

is_file

This function checks whether the specified file is a regular file. The function returns TRUE if it is a regular file otherwise it returns FALSE.

$file = "my_File.txt";
if(is_file($file)) {
  echo ("$file is a regular file");
} else {
  echo ("$file is not a regular file");
}

is_dir

The is_dir() function is very useful to checks whether the specified file is a directory or not. The function returns TRUE if the directory exists.

$file = "/dir/abc";
if(is_dir($file)) {
  echo ("$file is a directory");
} else  {
  echo ("$file is not a directory");
}

is_link

This is also a very handy function to checks whether the specified file is a link. This function returns TRUE if it is a link.


$file = "/dir/abc";
if(is_link($link)) {
	echo ("$link is a link");
} else  {
  echo ("$link is not a link");
}

file_exists

This function tells whether or not a file or directory exists. The function returns TRUE if the file or directory exists, otherwise it returns FALSE.

$filename = "/dir/test.txt";
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

You may also like: