2

I currently having a problem to validate the file name that user upload into the file input. I already had the validation on file extension, but when user trying to upload a file name example 1.hello.jpg, for my JavaScript validation code it was look okay as it was jpg extension. But in my PHP part my validation for the file extension does not recognize it as I'm using explode function to validate the file extension.

Is there anyway to validate the special character of the file in client side before submitted to my server side? Thanks

Here are my validation on client side:

    <script type='text/javascript'>

            function checkDisabled(yourSubmitButton){


              //No 1 file input validation  
              var res_field = document.getElementById('uploaded_file').value; 
              var extension = res_field.substr(res_field.lastIndexOf('.') + 1).toLowerCase();
              var allowedExtensions = ['jpg','JPG','bmp','BMP','png','PNG','jpeg','JPEG'];

              if(document.getElementById('bannerName').value.length !=0){
                  //File input extension validation
                  if (res_field.length > 0)
                 {
                      if (allowedExtensions.indexOf(extension) !== -1) 
                         {
                           yourSubmitButton.disabled = false;
                            return;
                         }
                }
              }



                // If we have made it here, disable it
                yourSubmitButton.disabled = true;

              };
              </script>     

Here are my php code for validate the extension:

$valid_formats = array("jpg", "JPEG","png","PNG","bmp","BMP",'JPG','jpeg');
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
            $name = $_FILES['uploaded_file']['name'];
            $size = $_FILES['uploaded_file']['size'];
            $bName = $_POST['bannerName'];
            $bDescription = $_POST['bannerDescription'];
            $bK = $_POST['bannerK'];
            $bKe = $_POST['bannerKe'];
            $bS= $_POST['bannerS'];

            if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {

                        ...........
                    }
                }
Kevin Andrid
  • 1,963
  • 1
  • 15
  • 26
Marcus Tan
  • 407
  • 1
  • 9
  • 26
  • 1.hello.jpg has two dots, by which you are trying to explode, so when exploding $ext has value hello which is invalid. – Sanjay Kumar N S May 13 '15 at 04:35
  • What if i uploaded a file `something.jpg.php` from what i'm reading this would upload and i could then execute my own server-side code on your server giving me access to pretty much everything and anything... – NewToJS May 13 '15 at 04:41
  • @newtojs What I understand from your statement, php file is unable to submit it to my server because my java script validation is only allowed image file – Marcus Tan May 13 '15 at 04:51
  • @sanjay Based on your knowledge which is the best solution to validate the file name and extension? Thanks – Marcus Tan May 13 '15 at 04:55
  • @MarcusTan Javascript is client-side and can be changed using the browser console. Example `allowedExtensions.push('php');` – NewToJS May 13 '15 at 05:07
  • @Marcus Tan try the below answer. – Sanjay Kumar N S May 13 '15 at 05:15

3 Answers3

2

In your PHP use PATHINFO_EXTENSION to get the file extension :

$ext = pathinfo($name, PATHINFO_EXTENSION);

For more info , check Here.

Community
  • 1
  • 1
Afsar
  • 3,104
  • 2
  • 25
  • 35
1

Try access the below value

$_FILES["uploaded_file"]["type"] 

and validate in server side.

Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
0

I had solve it by using split function on javascript.

Here is my code:

//Check for dot that is not more than 1
            var checkStr = 0;
            var str = document.getElementById('uploaded_file').value;
            var res = str.split(".");

            for(var i = 0 ; i <= res.length;i++){
                checkStr++;
            }

            if(checkStr>3){

                alert(str+' invalid filename detected. \n\n Please rename your file.');
                return false;
            }

For those beginner like me can refer to that, as i'm trying to split the filename that browser into the file input. If the array after split function is more than 3 then prompt out error.

Example :

  1. -1.hello.jpg (Error)
  2. -1_hello.jpg (Success)

Thanks for all the help. Much Appreciate

Marcus Tan
  • 407
  • 1
  • 9
  • 26