2016-08-29 2 views
0
<?php 
error_reporting(E_ALL); 
ini_set('display_errors' ,1); 

//expression to be found in file name 
$find = '.5010.'; 

//directory name 
//we will store renamed files here 
$dirname = '5010'; 
if(!is_dir($dirname)) 
    mkdir($dirname, 0777); 

//read all files from a directory 
//skip directories 
$directory_with_files = './'; 
$dh = opendir($directory_with_files); 
$files = array(); 
while (false !== ($filename = readdir($dh))) 
{ 
    if(in_array($filename, array('.', '..')) || is_dir($filename)) 
     continue; 

    $files[] = $filename; 
} 

//iterate collected files 
foreach($files as $file) 
{ 
    //check if file name is matching $find 
    if(stripos($file, $find) !== false) 
    { 

     //open file 
     $handle = fopen($file, "r"); 
     if ($handle) 
     { 
      //read file, line by line 
      while (($line = fgets($handle)) !== false) 
      { 

       //find REF line 
       $refid = 'REF*2U*'; 
       if(stripos($line, $refid) !== false) 
        { 
        //glue refernce numbers 
        //check if reference number is not empty 

        $refnumber = str_replace(array($refid, '~'), array('', ''), $line); 
       if($refnumber != '') 
        { 
       $refnumber = '_'. $refnumber .'_'; 

       $filerenamed = str_replace($find, $refnumber, $file); 
        copy($file, $dirname . '/' . $filerenamed); 
         } 

       echo $refnumber . "\n"; 
        } 
      } 

      //close file 
      fclose($handle); 
     } 
    } 
} 

?> 

이 코드는 출력이 ".5010"으로 대체되어야합니다. "ref"으로 코드를 실행했지만 코드를 실행할 때 파일 이름의 나머지 부분을 표시하지 않고 컴퓨터 퍼티에서 시도한 결과 "?" 심판 번호 다음에 내가 고칠 수있는 방법이 있니?예기치 않은 출력 파일명

예 : 내 파일이

코드가 실행하고 파일을 읽고 후

을 4867586.5010.476564.ed되고, 출력이되어야한다 : 4867586_SMIL01_476564.ed하지만 대신 : 나는 퍼티에 그것을 체크 아웃 때 파일 이름은 4867586_SMIL01

이었다 : 4867586_SMIL01? _476564.ed

+0

나는 이해하지 못한다. "?" 에서 오는? 그리고 무엇을 성취하려고합니까? 또한 현재 디렉토리 구조 및 파일의 예제, 현재 코드의 내용 및 원하는 모양을 제공해야합니다. –

+0

좋아요, 확인해주세요, 고쳤습니다. PHP의 이전 버전을 사용하고 있기 때문에 나는 그것을 생각합니다, 그래서 어쩌면 그게 문제이고, 그것을 고치는 방법이 있습니까? @Passerby –

+0

이 줄의 문제는 대부분 $ refnumber = str_replace (array ($ refid, '~'), array ('', ''), $ line);'입니다. .ed 파일에 '?'이 포함되어 있지 않음을 확인할 수 있습니까? 또는 잘못된 문자입니까? –

답변

1

파일 이름에 ?refnumber 어딘가에 aa non-printable 문자가 있음을 나타냅니다.

이것은 대부분 줄 바꿈 문자 또는 다른 것입니다.

$refnumber = str_replace(array($refid, '~'), array('', ''), $line); 

는, 당신이 중 하나를 사용하여 $refnumber 변수를 소독하기 위해 후자해야합니다 있다면

$refnumber = str_replace(array($refid, '~'), array('', ''), $line); 
$refnumber = trim($refnumber); // remove any whitespaces or line endings. 

에 : 그것은 전자의 경우 은, 그 라인을 변경하여 해결 될 수있다 온라인에서 사용할 수있는 파일 소독 기능.

+0

안녕하세요, 제 다른 질문을 해드릴 수 있습니까? –