A
A
Alexander Sobolev2021-02-03 12:44:10
PHP
Alexander Sobolev, 2021-02-03 12:44:10

How to count id3 tags of files on ftp?

Hello!

Legend
Переписываю функцию чтения id3 тэгов под работу с ftp. Если файл один - не проблема: перекачиваем во временный открываем извлекаем теги, удаляем и так далее.. НО! Как быть если нужно отсканировать ftp с 100500+ mp3 файлами.. ?

Attempt to read failed - or how to read remotely? What to do if the file is large or there are a lot of them.. how to replace fopen, how to optimize the process:
$iFSize = $iFSize =  ftp_size($this->connectionId, $sFilepath);
/* !!! */ $vFD = fopen($sFilepath,'r');   $sSrc = fread($vFD,$iFSize); fclose($vFD);

How to fix it?
Full code

public function listFile2($directory){
    // $valid_ext = array("mp3", "wav", "m3u"); 
    $list = ftp_mlsd($this->connectionId, $directory);
    if(!empty($list)){
      foreach ($list as $file) {
        switch ($file['type']) {
          case 'file': var_dump($this->getTagsInfo($directory.'/'.$file['name'])); break;
          case 'dir': $this->listFile2($directory.'/'.$file['name']); break;
        }
      }
    }	
    
    return $this->fileList;
  }

public function getTagsInfo($sFilepath) {
        $iFSize = filesize($sFilepath);  $vFD = fopen($sFilepath,'r'); $sSrc = fread($vFD,$iFSize); fclose($vFD);

        if (substr($sSrc,0,3) == 'ID3') {
            $aInfo['FileName'] = $sFilepath;
            $aInfo['Version'] = hexdec(bin2hex(substr($sSrc,3,1))).'.'.hexdec(bin2hex(substr($sSrc,4,1)));
        }

        if ($aInfo['Version'] == '4.0' || $aInfo['Version'] == '3.0') {
            for ($i = 0; $i < count($this->aTV23); $i++) {
                if (strpos($sSrc, $this->aTV23[$i].chr(0)) != FALSE) {

                    $s = '';
                    $iPos = strpos($sSrc, $this->aTV23[$i].chr(0));
                    $iLen = hexdec(bin2hex(substr($sSrc,($iPos + 5),3)));

                    $data = substr($sSrc, $iPos, 9 + $iLen);
                    for ($a = 0; $a < strlen($data); $a++) {
                        $char = substr($data, $a, 1);
                        if ($char >= ' ' && $char <= '~')
                            $s .= $char;
                    }
                    if (substr($s, 0, 4) == $this->aTV23[$i]) {
                        $iSL = 4;
                        if ($this->aTV23[$i] == 'USLT') {
                            $iSL = 7;
                        } elseif ($this->aTV23[$i] == 'TALB') {
                            $iSL = 5;
                        } elseif ($this->aTV23[$i] == 'TENC') {
                            $iSL = 6;
                        }
                        $aInfo[$this->aTV23t[$i]] = substr($s, $iSL);
                    }
                }
            }
        }

        if($aInfo['Version'] == '2.0') {
            for ($i = 0; $i < count($this->aTV22); $i++) {
                if (strpos($sSrc, $this->aTV22[$i].chr(0)) != FALSE) {

                    $s = '';
                    $iPos = strpos($sSrc, $this->aTV22[$i].chr(0));
                    $iLen = hexdec(bin2hex(substr($sSrc,($iPos + 3),3)));

                    $data = substr($sSrc, $iPos, 6 + $iLen);
                    for ($a = 0; $a < strlen($data); $a++) {
                        $char = substr($data, $a, 1);
                        if ($char >= ' ' && $char <= '~')
                            $s .= $char;
                    }

                    if (substr($s, 0, 3) == $this->aTV22[$i]) {
                        $iSL = 3;
                        if ($this->aTV22[$i] == 'ULT') {
                            $iSL = 6;
                        }
                        $aInfo[$this->aTV22t[$i]] = substr($s, $iSL);
                    }
                }
            }
        }
        return $aInfo;
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question