filetype filter does not work in php file upload

I want to let my users to upload image just in PNG and JPG anf JPEG filetypes . So I use this code in my html File :

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="utf-8" />
    <link href="assets/css/style.css" rel="stylesheet" />
    <script type="text/javascript" src='jquery.min.js' ></script>
    <script type="text/javascript">
    //<![CDATA[
        function upload_start(){
              document.getElementById('upload-process').style.visibility = 'visible';
              return true;
        }

        function upload_end(check_upload){
          var server_response = '';
          if (check_upload == 1){
             server_response = '<span class="ok">File Uploaded<\/span>';
          }
          else {
             server_response = '<span class="error">file Upload error<\/span>';
          }
          document.getElementById('upload-process').style.visibility = 'hidden';
          document.getElementById('upload-form').innerHTML = server_response;
          return true;   
    }
    //]]>
    </script>
</head>
<body>

            <form action="php-ajax-upload.php" method="post" enctype="multipart/form-data" target="upload-target" onsubmit="upload_start();">
            <label for="user-file"></label>
            <input type="file" id="user-file" name="user-file" />
            <input type="hidden" name="customer_id"  value="6000" />
            <input type="submit" value="Upload File" />
            </form>

And the php-ajax-upload.php content is :

<html>
<head>
<link href="assets/css/style.css" rel="stylesheet" />
</head>
<body>
<?php
$customer_id = $_POST['customer_id'] ;

if ( ($_FILES["user-file"]["type"] == "image/jpeg" )|| ($_FILES["user-file"]["type"] == "image/jpg" )|| ($_FILES["user-file"]["type"] == "image/png" ) ) 
{
    if ($_FILES["user-file"]["error"] > 0){
        echo "<div class=\"server\">Error: " . $_FILES["user-file"]["error"] . "</div><br />";
        $check_result = 0;
        }
    else{
        if (file_exists("user-upload/" . $_FILES["user-file"]["name"])){
            echo "<div class=\"server\">This file exists <br /><br />".$_FILES["user-file"]["name"]. "</div><br />";
            $check_result = 0;      
            }
        else{
                if(isset($_POST['customer_id']) && !empty($_POST['customer_id'])  )
                {
                    $customer_id = trim($_POST['customer_id']);

                    $filename  = basename($_FILES['user-file']['name']);
                    $format = pathinfo($filename, PATHINFO_EXTENSION);
                    $saved_file_name = $customer_id.'.'.$format ;
                    if(is_writable(dirname(__FILE__) . './admin-upload/'. $saved_file_name))
                    {
                      unlink(dirname(__FILE__) . './admin-upload/'. $saved_file_name ); 
                    }

                    if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpeg'))
                    {
                      unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpeg' ); 
                    }

                    if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpg'))
                    {
                      unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpg' ); 
                    }

                    if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.png'))
                    {
                      unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.png' ); 
                    }
                    move_uploaded_file($_FILES["user-file"]["tmp_name"],"admin-upload/".$customer_id.'.'.$format ) ;
                    $check_result = 1;

                }
                else
                {
                    echo 'Id not found !';
                    $check_result = 0;  
                }
            }
    }
}

else{
    if($_FILES["user-file"]["size"] > 1000000){
        echo "<div class='server red bold'>File is Large</div>";
    }
    else{
        echo "<div class='server red bold'> File Type is not Valid !</div>";
    }
    $check_result = 0;
}

?>


<script type="text/javascript">
window.top.window.upload_end(<?php echo $check_result; ?>);
</script>

</body>
</html>

now when I want to check my code , when I select an Imange ir works good ,also when I select other file types like : pdf , doc , srt , .... the code works good and will say : File Type is not Valid ! but only when I select an mp3 file , php-ajax-upload.php file errors :

Notice: Undefined index: customer_id in D:\Software\wamp\www\garanti\php-ajax-upload.php on line 7

Notice: Undefined index: user-file in D:\Software\wamp\www\garanti\php-ajax-upload.php on line 9

Notice: Undefined index: user-file in D:\Software\wamp\www\garanti\php-ajax-upload.php on line 9

Notice: Undefined index: user-file in D:\Software\wamp\www\garanti\php-ajax-upload.php on line 9

Notice: Undefined index: user-file in D:\Software\wamp\www\garanti\php-ajax-upload.php on line 64

And finally at the end of page says :

File Type is not Valid !

I dont Know why this error will happen only for mp3 files?

Answers


This sounds like it might be related to one of your php.ini settings.

I am assuming that the mp3 file is larger than any other files you have tried successfully to upload. If the size of the file is greater than the php.ini setting post_max_size then PHP will loose data from the post and I think that is what has happened here. The mp3 file may have uploaded, but the other variables that also come in the POST are being lost.

Check post_max_size its default value is 3Meg and you may have to increase it. Remember it will need to be > that upload_max_filesize to ensure you dont loose these other POST variiables.


Need Your Help

AJAX Masked Textbox custom mask

c# jquery asp.net web ajaxcontroltoolkit

I have a slider on my page and I want to set it up so that when the user slides (which is in minutes) it displays a 'pretty' version of the current selection. So, for example, if the slider is on '...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.