Read part of a file in PHP

前端 未结 3 1186
醉酒成梦
醉酒成梦 2021-01-24 00:51

I would like to read the last 1 megabyte of a MP3 file and calculate SHA1 checksum for just that part of the file. The reason that I would want this is that when I\'m looking fo

相关标签:
3条回答
  • 2021-01-24 01:09

    Try fseek. This will move the pointer to ~1024 kbytes before the end of the file.

     fseek($fp, -1024 * 1024, SEEK_END);
    
    0 讨论(0)
  • 2021-01-24 01:11

    You'd have to use the c wrappers for file manipulation: fopen, fseek and fread:

    $size = 1024 * 1000;
    $handle = fopen($file, 'r');
    fseek($handle, -$size);
    $limitedContent = fread($handle, $size);
    $hash = md5($limitedContent);
    
    0 讨论(0)
  • 2021-01-24 01:17

    MP3s don't have any inherent "header" info for song/album/artist. That's handled by ID3, which can either be at the front of the file (ID3v2, random size, depending on how much information has been specified) or at the end (ID3v1, fixed 128 bytes). To properly identify an MP3 by checksumming, you'd have to make sure that both versions of the ID3 tag are ignored. Furthermore, it's possible to have MP3s embedded in a .wav container, in which case there's .wav headers and whatnot.

    And of course, there's always the case of having two songs encoded with different bitrates, sampling rates, and even different CD rippers and encoders. All will produce utterly different files, but are still "the same song".

    0 讨论(0)
提交回复
热议问题