check if the file format is different with the file name extension in python, javascript?

  • Last Update :
  • Techknowledgy :

The usage is simple:

>>>
import magic
   >>>
   magic.from_file('renamed.pdf')
'ISO Media, Apple QuickTime movie'
# More handy
   >>>
   magic.from_file('renamed.pdf', mime = True)
'video/quicktime'
$('#movieFile').change(function() {
   var file = $('#movieFile')[0].files[0];

   var filename = file.name;
   var fileMimeType = file.type;
   var fileExtension = filename.split('.').pop();

   if (isValidMimeType(fileMimeType)) {
      console.log('good file');
   } else {
      console.log('bad file');
   }
});

function isValidMimeType(fileMimeType) {
   // mime type of .mov files
   var validFileMimeTypes = ['video/quicktime'];

   for (var i = 0; i < validFileMimeTypes.length; i++) {
      if (validFileMimeTypes[i].toLowerCase() === fileMimeType.toLowerCase()) {
         return true;
      }
   }
   return false;
}

adding to Mathias' answer using python-magic you could do this instead

file_type = magic.from_buffer(upload.file.read(1024), mime = True)

Suggestion : 2

In this example, you will learn to write a JavaScript program that will get the file extension of a given filename.,To understand this example, you should have the knowledge of the following JavaScript programming topics:,In the above program, the extension of the filename is extracted using the split() method and the pop() method.,In the above program, the extension of the filename is extracted using the substring() method and the lastIndexOf() method.

Example 1: Using split() and pop()

// program to get the file extension

function getFileExtension(filename) {

   // get file extension
   const extension = filename.split('.').pop();
   return extension;
}

// passing the filename
const result1 = getFileExtension('module.js');
console.log(result1);

const result2 = getFileExtension('module.txt');
console.log(result2);

Output

js
txt

Example 2: Using substring() and lastIndexOf()

// program to get the file extension

function getFileExtension(filename) {
   // get file extension
   const extension = filename.substring(filename.lastIndexOf('.') + 1, filename.length);
   return extension;
}

const result1 = getFileExtension('module.js');
console.log(result1);

const result2 = getFileExtension('test.txt');
console.log(result2);

Suggestion : 3

This code defines a variable called “fileName” and assigns it the value “sample.txt”. It then defines a variable called “fileExtension” and assigns it the value of the result of running the “split” method on the “fileName” variable, using a period (.) as the separator, and then running the “pop” method on that result. The net effect is that the “fileExtension” variable ends up containing the value “.txt”, which is the file extension of the “sample.txt” file.,The main problem with getting file extensions is that they can be very confusing. There are a lot of different file types and file extensions, and it can be hard to know which one to use.,You can also use copy structures in JavaScript to group related code together. For example, you might use a copy structure to store all of your variables in one place. You could then use that structure to access those variables from anywhere in your code., Save my name, email, and website in this browser for the next time I comment.

The main problem with getting file extensions is that they can be very confusing. There are a lot of different file types and file extensions, and it can be hard to know which one to use.

var fileName = "sample.txt";
var fileExtension = fileName.split('.').pop();

Suggestion : 4

You have to pass the filename to function. This function first checks the file extension and after that it returns true and false on the basis of the provided file extension array.,You can check if the file is an image using the filename in javascript. This function helps you get the file extension and return true or false if the file extension does not exist in the provided file extensions array.,W3codegenerator is the platform where web developers can ask queries and solve problems by submitting the code snippet for the queries and also generates the code. We provide articles and tutorials to solve coding problems in real world. , We use cookies to ensure that we give you the best experience on our website.

<script>
    function getFileExtension(fileName){
        var  fileExtension;
        fileExtension = fileName.replace(/^.*\./, '');
        return fileExtension;
    }
    function isIMage(fileName){
        var fileExt = getFileExtension(fileName);
        var imagesExtension = ["png", "jpg", "jpeg"];
        if(imagesExtension.indexOf(fileExt) !== -1){
            return true;
        } else{
            return false;
        }
    }
    console.log(isIMage('filename.jpg'));
</script>