Getting file extension in php

In this php tutorial you will learn how to get the file extension from a string in php. Sometimes during web development you may need to get the file extension, so let's have a look over how to do this.

There are different ways to do this. Let's have a look over each of them


1) Using explode()

The simplest and easiest way of getting file extension is to use the php built-in function explode.

The explode() function breaks a string into an array.

It takes three parameters in which two of them are required and one is optional.

a) The first required parameter is separator that specifies where to break the string.
b) The second required parameter is string that specifies the string to split.
c) The optional parameter is limit that specifies the maximum number of array elements to return.

So let's have a look over how to get the file extension using explode() method.

<?php
$filename="picture.jpg";
$fileArray=array();
$fileArray =explode('.',$filename);
$file_extension=$fileArray[1];
echo $file_extension;
?>


or you can made a custom php function and call explode() method in it and in the end return the value

<?php
function get_file_extension($file_name)
{
return end(explode('.',$file_name));
}


$filename="picture.jpg";
$file_extension=get_file_extension($filename);
echo $file_extension;
?>

2) Using PATHINFO_EXTENSION

Another way to get the file extension is to use the pathinfo which is an array that contains dirname, basename, extension and file name. Right now we are only interested in the file extension so I am going to call it directly.

<?php
$filename="picture.jpg";
$file_extension = pathinfo($filename,PATHINFO_EXTENSION);
echo $file_extension;
?>


Or you can do like this

<?php
function get_file_extension($file_name)
{
return pathinfo($file_name, PATHINFO_EXTENSION);
}

$filename="picture.jpg";
$file_extension =get_file_extension($filename);
echo $file_extension;
?>

3) Using substring and string char

You can use substring and string char to determine the file name length and position to get the file extension. Let’s have a look over how to do this

<?php
function get_file_extension($file_name)
{
return substr(strrchr($file_name,'.'),1);
}

$filename="picture.jpg";
$file_extension =get_file_extension($filename);
echo $file_extension;
?>

So these are the different ways to get the file extension in php.
Read more...

Redirection in javascript after some time delay

In this programming tutorial we will learn how to redirect the user to another web page or website after some time delay. Its quite easy in javascript as you have to write just single line of code. Lets have a look over the example given below.
Redirection in javascript after some time delay
For this purpose there is a built-in javascript method setTimeout().
setTimeout() method is used to repeatedly call some function after a specified amount of time.

Syntax of setTimeout() Method
setTimeout("Javascript Statement",Milliseconds);

Note: - The setTimeout() is the method of the HTML DOM Window object.

setTimeout() method takes two parameters. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('2 seconds!')" or a call to a function, like "helloMsg()".

The second parameter indicates how many milliseconds from now you want to execute the first parameter.

Note: - There are 1000 milliseconds in one second.

So lets have a look over the code
<html>
<head>
<title>Redirection in javascript after some time delay</title>
<script language="javascript" type="text/javascript">
function Delayer()
{
setTimeout('Redirection()', 5000);
}
function Redirection()
{
window.location="http://www.google.com";
}
</script>
</head>
<body>
<form>
<input type="button" value="Redirect after 5 secs" onclick="Delayer();" /></form>
</body>
</html>
The above mentioned code will redirect user to the google.com after 5 seconds of clicking the button. When user will click the button, the function Delayer() will be get call and in this function I am calling Redirection() function after every 5 seconds. So when focus will move to the Redirection() function after every 5 seconds then the user will be immediately redirected to the google.com

So that's it.
I love your feedback.
Read more...

How to disable the browser back button

In this javascript tutorial you will learn how to disable the back button on FireFox's browser or how to prevent the user to go back in FireFox’s browser. Although I am giving force to FireFox's browser yet this code works for all web browsers such as Google Chrome, Opera, Safari, Internet Explorer etc. The reason why I am giving force over FireFox because it is even hard in FireFox to disable the back button or to prevent user to go back.

For example, the below mentioned code will work in other web browsers such as Internet Explorer, Google Chrome but will not perform his responsibility incase of FireFox.


<script language="javascript" type="text/javascript">
window.history.forward(1);
</script>

Also this code will work in other browsers but in case of FireFox it will not work
<script language="javascript" type="text/javascript">
if(history.back)
{
history.go(+1)
}
</script>

So now let's have a look over javascript code that will disable the back button and prevent user to go back in Firefox and in all other browsers

<script language="javascript" type="text/javascript">
function noBack()
{
window.history.forward()
}
noBack();
window.inhibited_load=noBack;
window.onpageshow=function(evt){if(evt.persisted)noBack()}
window.inhibited_unload=function(){void(0)}
</script>

So this is the way to disable the back button on firefox and prevent user to go back.
Read more...