2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
9 // /demo/demo.write.php - part of getID3() //
10 // sample script for demonstrating writing ID3v1 and ID3v2 //
11 // tags for MP3, or Ogg comment tags for Ogg Vorbis //
12 // See readme.txt for more details //
14 /////////////////////////////////////////////////////////////////
17 die('Due to a security issue, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in '.$_SERVER['PHP_SELF']);
19 $TaggingFormat = 'UTF-8';
21 header('Content-Type: text/html; charset='.$TaggingFormat);
22 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
23 echo '<html><head><title>getID3() - Sample tag writer</title></head><style type="text/css">BODY,TD,TH { font-family: sans-serif; font-size: 9pt;" }</style><body>';
25 require_once('../getid3/getid3.php');
26 // Initialize getID3 engine
28 $getID3->setOption(array('encoding'=>$TaggingFormat));
30 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'write.php', __FILE__, true);
32 $browsescriptfilename = 'demo.browse.php';
34 $Filename = (isset($_REQUEST['Filename']) ? $_REQUEST['Filename'] : '');
38 if (isset($_POST['WriteTags'])) {
40 $TagFormatsToWrite = (isset($_POST['TagFormatsToWrite']) ? $_POST['TagFormatsToWrite'] : array());
41 if (!empty($TagFormatsToWrite)) {
42 echo 'starting to write tag(s)<BR>';
44 $tagwriter = new getid3_writetags;
45 $tagwriter->filename = $Filename;
46 $tagwriter->tagformats = $TagFormatsToWrite;
47 $tagwriter->overwrite_tags = false;
48 $tagwriter->tag_encoding = $TaggingFormat;
49 if (!empty($_POST['remove_other_tags'])) {
50 $tagwriter->remove_other_tags = true;
53 $commonkeysarray = array('Title', 'Artist', 'Album', 'Year', 'Comment');
54 foreach ($commonkeysarray as $key) {
55 if (!empty($_POST[$key])) {
56 $TagData[strtolower($key)][] = $_POST[$key];
59 if (!empty($_POST['Genre'])) {
60 $TagData['genre'][] = $_POST['Genre'];
62 if (!empty($_POST['GenreOther'])) {
63 $TagData['genre'][] = $_POST['GenreOther'];
65 if (!empty($_POST['Track'])) {
66 $TagData['track'][] = $_POST['Track'].(!empty($_POST['TracksTotal']) ? '/'.$_POST['TracksTotal'] : '');
69 if (!empty($_FILES['userfile']['tmp_name'])) {
70 if (in_array('id3v2.4', $tagwriter->tagformats) || in_array('id3v2.3', $tagwriter->tagformats) || in_array('id3v2.2', $tagwriter->tagformats)) {
71 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
73 if ($fd = fopen($_FILES['userfile']['tmp_name'], 'rb')) {
75 $APICdata = fread($fd, filesize($_FILES['userfile']['tmp_name']));
78 list($APIC_width, $APIC_height, $APIC_imageTypeID) = GetImageSize($_FILES['userfile']['tmp_name']);
79 $imagetypes = array(1=>'gif', 2=>'jpeg', 3=>'png');
80 if (isset($imagetypes[$APIC_imageTypeID])) {
82 $TagData['attached_picture'][0]['data'] = $APICdata;
83 $TagData['attached_picture'][0]['picturetypeid'] = $_POST['APICpictureType'];
84 $TagData['attached_picture'][0]['description'] = $_FILES['userfile']['name'];
85 $TagData['attached_picture'][0]['mime'] = 'image/'.$imagetypes[$APIC_imageTypeID];
88 echo '<b>invalid image format (only GIF, JPEG, PNG)</b><br>';
91 $errormessage = ob_get_contents();
93 echo '<b>cannot open '.$_FILES['userfile']['tmp_name'].'</b><br>';
96 echo '<b>!is_uploaded_file('.$_FILES['userfile']['tmp_name'].')</b><br>';
99 echo '<b>WARNING:</b> Can only embed images for ID3v2<br>';
103 $tagwriter->tag_data = $TagData;
104 if ($tagwriter->WriteTags()) {
105 echo 'Successfully wrote tags<BR>';
106 if (!empty($tagwriter->warnings)) {
107 echo 'There were some warnings:<BLOCKQUOTE STYLE="background-color:#FFCC33; padding: 10px;">'.implode('<br><br>', $tagwriter->warnings).'</BLOCKQUOTE>';
110 echo 'Failed to write tags!<BLOCKQUOTE STYLE="background-color:#FF9999; padding: 10px;">'.implode('<br><br>', $tagwriter->errors).'</BLOCKQUOTE>';
115 echo 'WARNING: no tag formats selected for writing - nothing written';
123 echo '<div style="font-size: 1.2em; font-weight: bold;">Sample tag editor/writer</div>';
124 echo '<a href="'.htmlentities($browsescriptfilename.'?listdirectory='.rawurlencode(realpath(dirname($Filename))), ENT_QUOTES).'">Browse current directory</a><br>';
125 if (!empty($Filename)) {
126 echo '<a href="'.htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES).'">Start Over</a><br><br>';
127 echo '<form action="'.htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES).'" method="post" enctype="multipart/form-data">';
128 echo '<table border="3" cellspacing="0" cellpadding="4">';
129 echo '<tr><th align="right">Filename:</th><td><input type="hidden" name="Filename" value="'.htmlentities($Filename, ENT_QUOTES).'"><a href="'.htmlentities($browsescriptfilename.'?filename='.rawurlencode($Filename), ENT_QUOTES).'" target="_blank">'.$Filename.'</a></td></tr>';
130 if (file_exists($Filename)) {
132 // Initialize getID3 engine
133 $getID3 = new getID3;
134 $OldThisFileInfo = $getID3->analyze($Filename);
135 getid3_lib::CopyTagsToComments($OldThisFileInfo);
137 switch ($OldThisFileInfo['fileformat']) {
141 $ValidTagTypes = array('id3v1', 'id3v2.3', 'ape');
145 $ValidTagTypes = array('ape');
149 if (!empty($OldThisFileInfo['audio']['dataformat']) && ($OldThisFileInfo['audio']['dataformat'] == 'flac')) {
150 //$ValidTagTypes = array('metaflac');
151 // metaflac doesn't (yet) work with OggFLAC files
152 $ValidTagTypes = array();
154 $ValidTagTypes = array('vorbiscomment');
159 $ValidTagTypes = array('metaflac');
163 $ValidTagTypes = array('real');
167 $ValidTagTypes = array();
170 echo '<tr><td align="right"><b>Title</b></td> <td><input type="text" size="40" name="Title" value="'.htmlentities((!empty($OldThisFileInfo['comments']['title']) ? implode(', ', $OldThisFileInfo['comments']['title'] ) : ''), ENT_QUOTES).'"></td></tr>';
171 echo '<tr><td align="right"><b>Artist</b></td><td><input type="text" size="40" name="Artist" value="'.htmlentities((!empty($OldThisFileInfo['comments']['artist']) ? implode(', ', $OldThisFileInfo['comments']['artist']) : ''), ENT_QUOTES).'"></td></tr>';
172 echo '<tr><td align="right"><b>Album</b></td> <td><input type="text" size="40" name="Album" value="'.htmlentities((!empty($OldThisFileInfo['comments']['album']) ? implode(', ', $OldThisFileInfo['comments']['album'] ) : ''), ENT_QUOTES).'"></td></tr>';
173 echo '<tr><td align="right"><b>Year</b></td> <td><input type="text" size="4" name="Year" value="'.htmlentities((!empty($OldThisFileInfo['comments']['year']) ? implode(', ', $OldThisFileInfo['comments']['year'] ) : ''), ENT_QUOTES).'"></td></tr>';
177 if (!empty($OldThisFileInfo['comments']['track_number']) && is_array($OldThisFileInfo['comments']['track_number'])) {
178 $RawTrackNumberArray = $OldThisFileInfo['comments']['track_number'];
179 } elseif (!empty($OldThisFileInfo['comments']['track']) && is_array($OldThisFileInfo['comments']['track'])) {
180 $RawTrackNumberArray = $OldThisFileInfo['comments']['track'];
182 $RawTrackNumberArray = array();
184 foreach ($RawTrackNumberArray as $key => $value) {
185 if (strlen($value) > strlen($TrackNumber)) {
186 // ID3v1 may store track as "3" but ID3v2/APE would store as "03/16"
187 $TrackNumber = $value;
190 if (strstr($TrackNumber, '/')) {
191 list($TrackNumber, $TracksTotal) = explode('/', $TrackNumber);
193 echo '<tr><td align="right"><b>Track</b></td><td><input type="text" size="2" name="Track" value="'.htmlentities($TrackNumber, ENT_QUOTES).'"> of <input type="text" size="2" name="TracksTotal" value="'.htmlentities($TracksTotal, ENT_QUOTES).'"></TD></TR>';
195 $ArrayOfGenresTemp = getid3_id3v1::ArrayOfGenres(); // get the array of genres
196 foreach ($ArrayOfGenresTemp as $key => $value) { // change keys to match displayed value
197 $ArrayOfGenres[$value] = $value;
199 unset($ArrayOfGenresTemp); // remove temporary array
200 unset($ArrayOfGenres['Cover']); // take off these special cases
201 unset($ArrayOfGenres['Remix']);
202 unset($ArrayOfGenres['Unknown']);
203 $ArrayOfGenres[''] = '- Unknown -'; // Add special cases back in with renamed key/value
204 $ArrayOfGenres['Cover'] = '-Cover-';
205 $ArrayOfGenres['Remix'] = '-Remix-';
206 asort($ArrayOfGenres); // sort into alphabetical order
207 echo '<tr><th align="right">Genre</th><td><select name="Genre">';
208 $AllGenresArray = (!empty($OldThisFileInfo['comments']['genre']) ? $OldThisFileInfo['comments']['genre'] : array());
209 foreach ($ArrayOfGenres as $key => $value) {
210 echo '<option value="'.htmlentities($key, ENT_QUOTES).'"';
211 if (in_array($key, $AllGenresArray)) {
212 echo ' selected="selected"';
213 unset($AllGenresArray[array_search($key, $AllGenresArray)]);
214 sort($AllGenresArray);
216 echo '>'.htmlentities($value).'</option>';
218 echo '</select><input type="text" name="GenreOther" size="10" value="'.htmlentities((!empty($AllGenresArray[0]) ? $AllGenresArray[0] : ''), ENT_QUOTES).'"></td></tr>';
220 echo '<tr><td align="right"><b>Write Tags</b></td><td>';
221 foreach ($ValidTagTypes as $ValidTagType) {
222 echo '<input type="checkbox" name="TagFormatsToWrite[]" value="'.$ValidTagType.'"';
223 if (count($ValidTagTypes) == 1) {
224 echo ' checked="checked"';
226 switch ($ValidTagType) {
230 if (isset($OldThisFileInfo['tags']['id3v2'])) {
231 echo ' checked="checked"';
236 if (isset($OldThisFileInfo['tags'][$ValidTagType])) {
237 echo ' checked="checked"';
242 echo '>'.$ValidTagType.'<br>';
244 if (count($ValidTagTypes) > 1) {
245 echo '<hr><input type="checkbox" name="remove_other_tags" value="1"> Remove non-selected tag formats when writing new tag<br>';
249 echo '<tr><td align="right"><b>Comment</b></td><td><textarea cols="30" rows="3" name="Comment" wrap="virtual">'.((isset($OldThisFileInfo['comments']['comment']) && is_array($OldThisFileInfo['comments']['comment'])) ? implode("\n", $OldThisFileInfo['comments']['comment']) : '').'</textarea></td></tr>';
251 echo '<tr><td align="right"><b>Picture</b><br>(ID3v2 only)</td><td><input type="file" name="userfile" accept="image/jpeg, image/gif, image/png"><br>';
252 echo '<select name="APICpictureType">';
253 $APICtypes = getid3_id3v2::APICPictureTypeLookup('', true);
254 foreach ($APICtypes as $key => $value) {
255 echo '<option value="'.htmlentities($key, ENT_QUOTES).'">'.htmlentities($value).'</option>';
257 echo '</select></td></tr>';
258 echo '<tr><td align="center" colspan="2"><input type="submit" name="WriteTags" value="Save Changes"> ';
259 echo '<input type="reset" value="Reset"></td></tr>';
263 echo '<tr><td align="right"><b>Error</b></td><td>'.htmlentities($Filename).' does not exist</td></tr>';
271 echo '</body></html>';