PHP: getting information about audio files _ php instance

Source: Internet
Author: User
Tags flock id3 talb
This article mainly introduces the relevant information of PHP to obtain audio files, which is very practical. If you need it, you can refer to it. Project requirement: Now there is an audio file upload function. After uploading, PHP needs to obtain information about the audio file, such as the duration, since this file is stored in the purchased space and cannot be processed by extensions like ffmpeg, can PHP obtain this information?

The following is an implementation of reading and writing audio file header information using PHP in the project. It is mainly for the WMA and MP3 formats for reference.

<? Php // AudioExif. class. php // Use PHP to read and write audio file header information. // currently, only the WMA and MP3 formats are supported. Only the following common headers are supported: title (name), Artist (Artist), Copyright (Copyright), Description (Description) // Year (AGE), Genre (Genre), AlbumTitle (album Title) // here, mp3 and wma are slightly different. More information may be returned, but only the above information can be written. // mp3 also supports Track (Track number Writing) // For MP3 files, ID3v1 and ID3v2 are supported. v2 is preferred when reading files. v1 is always written when writing files. If necessary, v2 is written. // usage instructions: (Since wma uses Unicode access, mb_convert_encoding () is also required to expand // return data and write data. The data is ANSI encoded, that is, the data is displayed when the data is stored (Chinese _ GB2312) // require ('audioexif. class. php '); // $ AE = new AudioExif; // $ file ='/path/to/test10000'; // 1. check whether the file is complete (only for wma, mp3 always returns true) /// $ AE-> CheckSize ($ file); // 2. read information. The returned value is an array composed of information. For key name explanations, see // print_r ($ AE-> GetInfo ($ file); // 3. write information. The second parameter is a hash array, key-> value. For more information, see the preceding section, mp3 also supports Track // requires that the file path of the first parameter can be written by this program // $ pa = array ('title' => 'newtitle ', 'albumtitle' => 'new album name '); // $ AE-> SetInfo ($ file, $ pa); // others: the plug-in spent a lot of time searching for wma and mp3 file formats and web pages, I hope it will be useful to you. // In fact, there are already a lot of similar programs on the Internet, but there are too few for wma, and they can only be operated through the M $ // API on the win platform, MP3 is rarely available in unix/linux Command lines, so I wrote this module // if I found a bug or submitted a patch, or improved it to make it more robust, please tell me. // (for the file formats and structures of ID3 and Wma, you can find references on the Internet.) // if (! Extension_loaded ('mbstring') {trigger_error ('php Extension module 'mbstring' is required for AudioExif ', E_USER_WARNING); return true ;} // the Main Classclass AudioExif {// public varsvar $ _ wma = false; var $ _ mp3 = false; // Constructfunction AudioExif () {// nothing to do} // check the filesizefunction CheckSize ($ file) {$ handler = & $ this-> _ get_handler ($ file); if (! $ Handler) return false; return $ handler-> check_size ($ file);} // get the infomationsfunction GetInfo ($ file) {$ handler = & $ this-> _ get_handler ($ file); if (! $ Handler) return false; return $ handler-> get_info ($ file);} // write the infomationsfunction SetInfo ($ file, $ pa) {if (! Is_writable ($ file) {trigger_error ('audioexif: file ''. $ file. ''can not been overwritten ', E_USER_WARNING); return false;} $ handler = & $ this-> _ get_handler ($ file); if (! $ Handler) return false; return $ handler-> set_info ($ file, $ pa);} // private methodsfunction & _ get_handler ($ file) {$ ext = strtolower (strrchr ($ file ,'. '); $ ret = false; if ($ ext = 'hangzhou') {// MP3 $ ret = & $ this-> _ mp3; if (! $ Ret) $ ret = new _ Mp3Exif ();} else if ($ ext = '.wma ') {// wma $ ret = & $ this-> _ wma; if (! $ Ret) $ ret = new _ WmaExif ();} else {// unknowntrigger_error ('audioexif not Ted orted ''. $ ext. ''file. ', E_USER_WARNING) ;}return $ ret ;}// DBCS => gb2312function dbcs_gbk ($ str) {// strip the last "\ 0 \ 0" $ str = substr ($ str, 0,-2); return mb_convert_encoding ($ str, 'gbk ', 'ucs-2le');} // gb2312 => DBCSfunction gbk_dbcs ($ str) {$ str = mb_convert_encoding ($ str, 'ucs-2le', 'gbk '); $ str. = "\ 0 \ 0"; return $ str;} // File exifclass _ AudioExif {var $ fd; var $ head; var $ head_off; var $ head_buf; // init the file handlerfunction _ file_init ($ fpath, $ write = false) {$ mode = ($ write? 'Rb + ': 'rb'); $ this-> fd = @ fopen ($ fpath, $ mode); if (! $ This-> fd) {trigger_error ('audioexif :''. $ fpath. ''can not be opened with mode ''. $ mode. ''', E_USER_WARNING); return false ;}$ this-> head = false; $ this-> head_off = 0; $ this-> head_buf = ''; return true ;} // read buffer from the head_buf & move the off pointerfunction _ read_head_buf ($ len) {if ($ len <= 0) return NULL; $ buf = substr ($ this-> head_buf, $ this-> head_off, $ len); $ this-> head_off + = strlen ($ buf); Return $ buf;} // read one short valuefunction _ read_head_short () {$ ord1 = ord (substr ($ this-> head_buf, $ this-> head_off, 1 )); $ ord2 = ord (substr ($ this-> head_buf, $ this-> head_off + 1, 1); $ this-> head_off + = 2; return ($ ord1 + ($ ord2 <8);} // save the file headfunction _ file_save ($ head, $ olen, $ nlen = 0) {if ($ nlen = 0) $ nlen = strlen ($ head); if ($ nlen = $ olen) {// shorterflock ($ this-> fd, LOCK_EX); fseek ($ th Is-> fd, 0, SEEK_SET); fwrite ($ this-> fd, $ head, $ nlen); flock ($ this-> fd, LOCK_UN );} else {// longer, buffer required $ stat = fstat ($ this-> fd); $ fsize = $ stat ['SIZE']; // buf required( 4096 ?) It should not be nlen-olen> 4096 $ woff = 0; $ roff = $ olen; // read first bufferflock ($ this-> fd, LOCK_EX ); fseek ($ this-> fd, $ roff, SEEK_SET); $ buf = fread ($ this-> fd, 4096); // seek to startfseek ($ this-> fd, $ woff, SEEK_SET); fwrite ($ this-> fd, $ head, $ nlen); $ woff + = $ nlen; // seek to woff & write the datado {$ buf2 = $ buf; $ roff + = 4096; if ($ roff <$ fsize) {fseek ($ this-> fd, $ roff, SEEK_SET); $ buf = fread ($ this-> fd, 4096 );} // Save last buffer $ len2 = strlen ($ buf2); fseek ($ this-> fd, $ woff, SEEK_SET); fwrite ($ this-> fd, $ buf2, $ len2); $ woff + = $ len2;} while ($ roff <$ fsize); ftruncate ($ this-> fd, $ woff ); flock ($ this-> fd, LOCK_UN) ;}// close the filefunction _ file_deinit () {if ($ this-> fd) {fclose ($ this-> fd); $ this-> fd = false ;}}} // wma classclass _ WmaExif extends _ AudioExif {var $ items1 = array ('title', 'artlist', 'copyright', 'd Escription ', 'reserved'); var $ items2 = array ('Year', 'genre', 'albumtitle'); // check file size (length) maybe invalid filefunction check_size ($ file) {$ ret = false; if (! $ This-> _ file_init ($ file) return true; if ($ this-> _ init_header () {$ buf = fread ($ this-> fd, 24 ); $ tmp = unpack ('h32id/Vlen/h8unused', $ buf); if ($ tmp ['id'] = '3626b2758e66cf11a6d900aa0062ce6c ') {$ stat = fstat ($ this-> fd ); $ ret = ($ stat ['SIZE'] = ($ this-> head ['len'] + $ tmp ['len']);} $ this-> _ file_deinit (); return $ ret;} // set info (save the infos) function set_info ($ file, $ pa) {// check the pasettype ($ pa, 'a Rray'); if (! $ This-> _ file_init ($ file, true) return false; if (! $ This-> _ init_header () {$ this-> _ file_deinit (); return false;} // parse the old header & generate the new header $ head_body = ''; $ st_found = $ ex_found = false; $ head_num = $ this-> head ['num']; while ($ tmp = $ this-> _ get_head_frame ()) & ($ head_num> 0) {$ head_num --; if ($ tmp ['id'] = '3326b2758e66cf11a6d900aa0062ce6c ') {// Standard Info // 1-4 $ st_found = true; $ st_body1 = $ st_body2 = ''; $ lenx = unpack ('v5 ', $ This-> _ read_head_buf (10); $ tmp ['len']-= 34; // 10 + 24for ($ I = 0; $ I <count ($ this-> items1); $ I ++) {$ l = $ lenx [$ I + 1]; $ k = $ this-> items1 [$ I]; $ tmp ['len']-= $ l; $ data = $ this-> _ read_head_buf ($ l ); if (isset ($ pa [$ k]) $ data = gbk_dbcs ($ pa [$ k]); $ st_body2. = $ data; $ st_body1. = pack ('V', strlen ($ data);} // left lengthif ($ tmp ['len']> 0) $ st_body2. = $ this-> _ read_head_buf ($ tmp ['len']); // save to head_body $ Head_body. = pack ('h32vh8', $ tmp ['id'], strlen ($ st_body1. $ st_body2) + 24, $ tmp ['unused']); $ head_body. = $ st_body1. $ st_body2; $ st_body2. = $ data;} // save to head_body $ head_body. = pack ('h32va4', '3326b2758e66cf11a6d900aa0062ce6c', strlen ($ st_body1. $ st_body2) + 24, ''); $ head_body. = $ st_body1. $ st_body2; $ this-> head ['num'] ++;} // ex not found? If (! $ Ex_found) {$ inum = 0; $ et_body = ''; foreach ($ this-> items2 as $ k) {$ nbuf = gbk_dbcs ('wm /'. $ k); $ vbuf = (isset ($ pa [$ k])? Gbk_dbcs ($ pa [$ k]): ""); $ et_body. = pack ('V', strlen ($ nbuf )). $ nbuf. pack ('vv ', 0, strlen ($ vbuf )). $ vbuf; $ inum ++;} $ head_body. = pack ('h32va4v ', '40a4d0d207e3d21197f000a0c95ea850', strlen ($ et_body) + 26, '', $ inum); $ head_body. = $ et_body; $ this-> head ['num'] ++;} // after save $ new_len = strlen ($ head_body) + 30; $ old_len = $ this-> head ['len']; if ($ new_len <$ old_len) {$ head_body. = str_repeat ("\ 0", $ old_l En-$ new_len); $ new_len = $ old_len;} $ tmp = $ this-> head; $ head_buf = pack ('h32vvvh4 ', $ tmp ['id'], $ new_len, $ tmp ['len2'], $ tmp ['num'], $ tmp ['unused']); $ head_buf. = $ head_body; $ this-> _ file_save ($ head_buf, $ old_len, $ new_len); // close the file & return $ this-> _ file_deinit (); return true;} // get infofunction get_info ($ file) {$ ret = array (); if (! $ This-> _ file_init ($ file) return false; if (! $ This-> _ init_header () {$ this-> _ file_deinit (); return false ;} // get the data from head_buf $ head_num = $ this-> head ['num']; // num of head_framewhile ($ tmp = $ this-> _ get_head_frame ()) & $ head_num> 0) {$ head_num --; if ($ tmp ['id'] = '3326b2758e66cf11a6d900aa0062ce6c ') {// Standard Info $ lenx = unpack ('v * ', $ this-> _ read_head_buf (10); for ($ I = 1; $ I <= count ($ this-> items1); $ I ++) {$ k = $ this-> items1 [$ I-1]; $ Ret [$ k] = dbcs_gbk ($ this-> _ read_head_buf ($ lenx [$ I]);} else if ($ tmp ['id'] = '40a4d0d207e3d21197f000a0c95ea850 ') {// Extended Info $ inum = $ this-> _ read_head_short (); $ tmp ['len']-= 26; while ($ inum> 0 & $ tmp ['len']> 0) {// attribute name $ nlen = $ this-> _ read_head_short (); $ nbuf = $ this-> _ read_head_buf ($ nlen ); // the flag & value length $ flag = $ this-> _ read_head_short (); $ vlen = $ this-> _ read_head_shor T (); $ vbuf = $ this-> _ read_head_buf ($ vlen ); // update the XX $ tmp ['len']-= (6 + $ nlen + $ vlen); $ inum --; $ name = dbcs_gbk ($ nbuf ); $ k = substr ($ name, 3); if (in_array ($ k, $ this-> items2) {// all is string value (refer to falg for other tags) $ ret [$ k] = dbcs_gbk ($ vbuf) ;}} else {// skip onlyif ($ tmp ['len']> 24) $ this-> head_off + = ($ tmp ['len']-24) ;}$ this-> _ file_deinit (); return $ ret ;} // get the header? Function _ init_header () {fseek ($ this-> fd, 0, SEEK_SET); $ buf = fread ($ this-> fd, 30 ); if (strlen ($ buf )! = 30) return false; $ tmp = unpack ('h32id/Vlen/Vlen2/Vnum/h4unused', $ buf); if ($ tmp ['id']! = '3026b2758e66cf11a6d900aa0062ce6c ') return false; $ this-> head_buf = fread ($ this-> fd, $ tmp ['len']-30 ); $ this-> head = $ tmp; return true;} // _ get_head_frame () function _ get_head_frame () {$ buf = $ this-> _ read_head_buf (24 ); if (strlen ($ buf )! = 24) return false; $ tmp = unpack ('h32id/Vlen/h8unused', $ buf); return $ tmp ;}} // mp3 class (if not IDv2 then select IDv1) class _ Mp3Exif extends _ AudioExif {var $ head1; var $ genres = array ('Blues', 'classic Rock ', 'country', 'dance ', 'disco', 'funk', 'grunge', 'hip-Hop', 'jazz', 'metal', 'new age ', 'oldies', 'other', 'pop', 'r & B ', 'rap', 'reggae', 'Rock ', 'techno', 'industrial ', "Alternative", "Ska", "Death Metal", "Pranks ',' Soundtrack', 'Euro-techno', 'ambient', 'trip-Hop ', 'vocal', 'jazz + funk', 'fusion ', 'trance ', 'classic', 'instrumental ', 'Acid', 'house', 'game', 'sound clip', 'gospel', 'noise ', 'alternrock ', 'batch', 'Soul ', 'punk', 'space', 'meditative', 'instrumental Pop', 'instrumental Rock', 'ethenic', 'gothic ', 'dark', 'techno-industrial', 'electronic ', 'Pop-folk', 'eurodance', 'dream', 'Southern Rock', 'comedy ', 'cresult', 'gangsta ', 'top 40 ', 'Christian rap', 'Pop/funk', 'jungl', 'native American', 'cabaret ', 'new Wave', 'psychadelic ', 'rdave ', 'showinstances', 'trailer', 'Lo-Fi ', 'trigger', 'Acid punk', 'Acid jazz', 'polka', 'retro', 'Musical ', 'Rock & roll', 'hard Rock', 'unknown '); // MP3 always return truefunction check_size ($ file) {return true ;} // get infofunction get_info ($ file) {if (! $ This-> _ file_init ($ file) return false; $ ret = false; if ($ this-> _ init_header ()) {$ ret = ($ this-> head? $ This-> _ get_v2_info (): $ this-> _ get_v1_info (); $ ret ['meta'] = $ this-> _ get_meta_info ();} $ this-> _ file_deinit (); return $ ret;} // set infofunction set_info ($ file, $ pa) {if (! $ This-> _ file_init ($ file, true) return false; if ($ this-> _ init_header ()) {// always save v1 info $ this-> _ set_v1_info ($ pa); // set v2 first if need $ this-> _ set_v2_info ($ pa );} $ this-> _ file_deinit (); return true;} // get the header information [v1 + v2], call after file_initfunction _ init_header () {$ this-> head1 = false; $ this-> head = false; // try to get ID3v1 firstfseek ($ this-> fd,-128, SEEK_END ); $ buf = fread ($ thi S-> fd, 128); if (strlen ($ buf) == 128 & substr ($ buf, 0, 3) = 'tag ') {$ tmp = unpack ('a3id/a30Title/a30Artist/a30AlbumTitle/a4Year/a28Description/CReserved/CTrack/cgenre', $ buf); $ this-> head1 = $ tmp ;} // try to get ID3v2fseek ($ this-> fd, 0, SEEK_SET); $ buf = fread ($ this-> fd, 10); if (strlen ($ buf) = 10 & substr ($ buf, 0, 3) = 'id3 ') {$ tmp = unpack ('a3id/Cver/Crev/Cflag/C4size ', $ buf); $ tmp ['SIZE'] = ($ tmp ['Size2'] <21) | ($ tmp ['size2'] <14) | ($ tmp ['size3'] <7) | $ tmp ['size4']; unset ($ tmp ['size2'], $ tmp ['size2'], $ tmp ['size3'], $ tmp ['size4']); $ this-> head = $ tmp; $ this-> head_buf = fread ($ this-> fd, $ tmp ['SIZE']);} return ($ this-> head1 | $ this-> head);} // get v1 infofunction _ get_v1_info () {$ ret = array (); $ tmpa = array ('title', 'artlist', 'copyright', 'description', 'Year', 'albumtitle '); foreach ($ tmpa as $ tmp ){ $ Ret [$ tmp] = $ this-> head1 [$ tmp]; if ($ pos = strpos ($ ret [$ tmp], "\ 0 ")) $ ret [$ tmp] = substr ($ ret [$ tmp], 0, $ pos);} // count the Genre, [Track] if ($ this-> head1 ['reserved'] = 0) $ ret ['track'] = $ this-> head1 ['track']; else $ ret ['description']. = chr ($ ret ['reserved']). chr ($ ret ['track']); // Genre_idx $ g = $ this-> head1 ['genre']; if (! Isset ($ this-> genres [$ g]) $ ret ['genre'] = 'unknown '; else $ ret ['genre'] = $ this-> genres [$ g]; // return the value $ ret ['id3v1 '] = 'yes '; return $ ret;} // get v2 infofunction _ get_v2_info () {$ ret = array (); $ items = array ('tac' => 'copyright ', 'tpe1' => 'artlist', 'tit2' => 'title', 'trck' => 'track', 'tacon' => 'genre ', 'comm '=> 'description', 'tyer' => 'Year', 'talb' => 'albumtitle'); while (true) {$ buf = $ this-> _ read_hea D_buf (10); if (strlen ($ buf )! = 10) break; $ tmp = unpack ('a4fid/Nsize/nflag ', $ buf); if ($ tmp ['SIZE'] = 0) break; $ tmp ['dat '] = $ this-> _ read_head_buf ($ tmp ['SIZE']); // 0x6000 (11000000 00000000) if ($ tmp ['flag'] & 0x6000) continue; // mapping the dataif ($ k = $ items [$ tmp ['fid']) {// If first char is "\ 0", just skipif (substr ($ tmp ['dat '], 0, 1) = "\ 0 ") $ tmp ['dat '] = substr ($ tmp ['dat'], 1); $ ret [$ k] = $ tmp ['dat '];} // reset the genre If ($ g = $ ret ['genre']) {if (substr ($ g,) = '(' & substr ($ g) = ') $ g = substr ($ g, 1,-1); if (is_numeric ($ g) {$ g = intval ($ g ); $ ret ['genre'] = (isset ($ this-> genres [$ g])? $ This-> genres [$ g]: 'unknown ') ;}}$ ret ['id3v1'] = 'no'; return $ ret ;} // get meta info of MP3function _ get_meta_info () {// seek to the lead buf: 0xff $ off = 0; if ($ this-> head) $ off = $ this-> head ['SIZE'] + 10; fseek ($ this-> fd, $ off, SEEK_SET); while (! Feof ($ this-> fd) {$ skip = ord (fread ($ this-> fd, 1); if ($ skip = 0xff) break ;} if ($ skip! = 0xff) return false; $ buf = fread ($ this-> fd, 3); if (strlen ($ buf )! = 3) return false; $ tmp = unpack ('c3', $ buf); if ($ tmp [1] & 0xf0 )! = 0xf0) return false; // get the meta info $ meta = array (); // get mpeg version $ meta ['mpeg '] = ($ tmp [1] & 0x08? 1: 2); $ meta ['player'] = ($ tmp [1] & 0x04 )? ($ Tmp [1] & 0x02 )? 1: 2): ($ tmp [1] & 0x02 )? 3: 0); $ meta ['epro'] = ($ tmp [1] & 0x01 )? 'No': 'yes'; // bit rates $ bit_rates = array (1 => array (1 => array (128,160,192,224,256,288,320,352,384,416,448, 0 ), 2 => array (96,112,128,160,192,224,256,320,384, 112,128,160,192,224,256,320, 0), 3 => array (, 0 )), 2 => array (1 => array (96,112,128,144,160,176,192,224,256, 96,112,128,144,160, 0), 2 => array, 0), 3 => array (96,112,128,144,160,); $ I = $ meta ['mpeg ']; $ j = $ meta ['player']; $ k = ($ tmp [2]> 4 ); $ meta ['bitrate'] = $ bit_rates [$ I] [$ j] [$ k]; // sample rates
 <采样率>
  
$ Sam_rates = array (1 => array (44100,48000, 32000,0), 2 => array (22050,24000, 16000,0 )); $ meta ['samrate'] = $ sam_rates [$ I] [$ k]; $ meta ["padding"] = ($ tmp [2] & 0x02 )? 'On': 'off'; $ meta ["private"] = ($ tmp [2] & 0x01 )? 'On': 'off'; // mode & mode_ext $ k = ($ tmp [3]> 6); $ channel_modes = array ('stereo ', 'Joint stereo ', 'dual channel', 'single channel'); $ meta ['Mode'] = $ channel_modes [$ k]; $ k = ($ tmp [3]> 4) & 0x03); $ extend_modes = array ('mpg _ MD_LR_LR ', 'mpg _ MD_LR_ I ', 'mpg _ MD_MS_LR ', 'mpg _ MD_MS_ I'); $ meta ['ext _ mode'] = $ extend_modes [$ k]; $ meta ['copyright'] = ($ tmp [3] & 0x08 )? 'Yes': 'no'; $ meta ['original'] = ($ tmp [3] & 0x04 )? 'Yes': 'no'; $ emphasis = array ('none', '2017 microsecs', 'rreserved', 'ccitt J 17 '); $ k = ($ tmp [3] & 0x03); $ meta ['phasis '] = $ emphasis [$ k]; return $ meta ;} // set v1 infofunction _ set_v1_info ($ pa) {// ID3v1 (simpled) $ off =-128; if (! ($ Tmp = $ this-> head1) {$ off = 0; $ tmp ['id'] = 'tag '; $ tmp ['title'] = $ tmp ['artlist'] = $ tmp ['albumtitle'] = $ tmp ['Year'] = $ tmp ['description'] = ''; $ tmp ['reserved'] = $ tmp ['track'] = $ tmp ['genre'] = 0;} // basic items $ items = array ('title ', 'artlist', 'copyright', 'description', 'Year', 'albumtitle'); foreach ($ items as $ k) {if (isset ($ pa [$ k]) $ tmp [$ k] = $ pa [$ k];} // genre indexif (isset ($ pa ['genre']) {$ g = 0; foreach ($ this-> genres as $ gtmp) {if (! Strcasecmp ($ gtmp, $ pa ['genre']) break; $ g ++;} $ tmp ['genre'] = $ g ;} if (isset ($ pa ['track']) $ tmp ['track'] = intval ($ pa ['track']); // pack the data $ buf = pack ('a3a30a30a30a4a28ccc ', $ tmp ['id'], $ tmp ['title'], $ tmp ['artlist'], $ tmp ['albumtitle'], $ tmp ['Year'], $ tmp ['description'], 0, $ tmp ['track'], $ tmp ['genre']); flock ($ this-> fd, LOCK_EX); fseek ($ this-> fd, $ off, SEEK_END ); fwrite ($ this-> fd, $ buf, 128); flock ($ This-> fd, LOCK_UN);} // set v2 infofunction _ set_v2_info ($ pa) {if (! $ This-> head) {// insert ID3return; // if no value exists, forget it./** $ tmp = array ('id' => 'id3 ', 'ver '=> 3, 'Rev' => 0, 'flag' => 0); $ tmp ['SIZE'] =-10; // + 10 => 0 $ this-> head = $ tmp; $ this-> head_buf = ''; $ this-> head_off = 0; **/} $ items = array ('tacop' => 'copyright', 'tpe1' => 'artlist', 'tit2' => 'title ', 'trac' => 'track', 'tacon' => 'genre', 'comm '=> 'description', 'tyer' => 'Year ', 'talb' => 'albumtitle'); $ head_body = ''; while (true) {$ buf = $ thi S-> _ read_head_buf (10); if (strlen ($ buf )! = 10) break; $ tmp = unpack ('a4fid/Nsize/nflag ', $ buf); if ($ tmp ['SIZE'] = 0) break; $ data = $ this-> _ read_head_buf ($ tmp ['SIZE']); if ($ k = $ items [$ tmp ['fid']) & isset ($ pa [$ k]) {// the data shocould prefix by "\ 0" [replace] $ data = "\ 0 ". $ pa [$ k]; unset ($ pa [$ k]);} $ head_body. = pack ('a4nn ', $ tmp ['fid'], strlen ($ data), $ tmp ['flag']). $ data;} // reverse the items & set the new tags $ items = array_flip ($ items); foreach ($ pa as $ k => $ v) {if ($ fid = $ items [$ k]) {$ head_body. = pack ('a4nn ', $ fid, strlen ($ v) + 1, 0 ). "\ 0 ". $ v ;}// new length $ new_len = strlen ($ head_body) + 10; $ old_len = $ this-> head ['SIZE'] + 10; if ($ new_len <$ old_len) {$ head_body. = str_repeat ("\ 0", $ old_len-$ new_len); $ new_len = $ old_len;} // count the size1, 2, 3, 4, no include the header // abnormal algorithm...: p (28 bytes integer) $ size = array (); $ nlen = $ new_len-10; for ($ I = 4; $ I> 0; $ I --) {$ size [$ I] = ($ nlen & 0x7f); $ nlen >>=7;} $ tmp = $ this-> head; // echo "old_len: $ old_len new_len: $ new_len \ n "; $ head_buf = pack ('a3ccccccc ', $ tmp ['id'], $ tmp ['ver'], $ tmp ['Rev '], $ tmp ['flag'], $ size [1], $ size [2], $ size [3], $ size [4]); $ head_buf. = $ head_body; $ this-> _ file_save ($ head_buf, $ old_len, $ new_len );}
 

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.