Read part of a file in PHP

假如想象 提交于 2020-01-10 05:55:06

问题


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 for duplicate MP3's, the header info (song title, album etc.) can differ even though it's the exakt same audio file, so I figured I would be better of to checksum a part of the file at the end instead of the whole one. Is there an efficient way of doing this?


回答1:


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);



回答2:


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".




回答3:


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

 fseek($fp, -1024 * 1024, SEEK_END);


来源:https://stackoverflow.com/questions/2478102/read-part-of-a-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!