HOME BLOG PORTFOLIO PHOTO CONTACT Admin Login
javascript, js, php code for checking file extension

Javascript

 

<script language=”javascript” type=”text/javascript”>

if(document.form1.product_image.value!= “”)
{
var ext = document.form1.product_image.value;
ext = ext.substring(ext.length-3,ext.length);
ext = ext.toLowerCase();
if((ext != ‘jpg’)&&(ext != ‘gif’)&&(ext != ‘png’)&&(ext != ‘peg’))
{
alert(‘You selected a .’+ ext + ‘ file; n please select a .jpg/.gif/.png/.peg image file’);
document.form1.product_image.focus();
document.form1.product_image.value=”";
return false;
}
}

</script>

 

PHP method1 (this is good)

 

<?php
//This php code for show your urls dirname,basename,extension,filename with 2 methods
// This work PHP 5.2.0 version or +
// save below line code aoutus.php or any name no problem
echo “<br>Full Url=”.$fullUrl = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$path_parts = pathinfo($fullUrl);
echo “<br>Dir name=”.$path_parts['dirname'];
echo “<br>Base name method1=”.$path_parts['basename'];//good
echo “<br>Base name method2=”.basename($_SERVER['PHP_SELF']);//not good
echo “<br>Extension method1=”.$path_parts['extension'];//good
echo “<br>Extension method2=”. end(explode(‘.’, basename($_SERVER['PHP_SELF'])));//not good when abc.txt.jpg (like this name file uploaded)
echo “<br>filename without extension method1=”.$path_parts['filename'];//good
echo “<br>filename without extension method2=”. current(explode(‘.’, basename($_SERVER['PHP_SELF'])));//not good what happen if file name abc.doc.txt
/*
output like This
Full Url=localhost/jay/17pathinfo/aboutus.php
Dir name=localhost/jay/17pathinfo
Base name method1=aboutus.php
Base name method2=aboutus.php
Extension name method1=php
Extension name method2=php
filename name method1=aboutus
filename name method2=aboutus
*/
?>

PHP method2 (not good)

 

<?php

$ext = end(explode(‘.’, strtoupper($_FILES[image][name])));
if(($ext!=’JPG’)&&($ext!=’jpg’)&&($ext!=’JPEG’)&&($ext!=’jpeg’)&&($ext!=’GIF’)&&($ext!=’gif’)&&($ext!=’PNG’)&&($ext!=’png’))
{
$msg1.=”<br>image Not uploaded, Try to upload only JPG/JPEG/GIF/PNG”;
$flag=0;
}
if(($_FILES["image"]["size"])>=5000000) //500 kb *1000
{
$msg1.=”<br>image Not uploaded, Try to upload only below 500kb”;
$flag=0;
}
if(($_FILES["image"]["error"])!=0)
{
$msg1.=”<br>image Not uploaded, Some Un-Expected error”;
$flag=0;
}

?>

 

   Share on Facebook

Page views:141