2016-08-26 3 views
0

question을 참조하면 filemtime();을 사용하여 폴더 내의 파일을 마지막으로 수정 한 시간을 확인하는 데 어려움이 있음을 발견했습니다.filemtime을 사용하여 마지막으로 수정 한 시간까지 모든 파일을 이동하는 방법

__DIR__ .'/../uploads/media';에있는 모든 파일을 마지막 수정 시간을 기준으로 __DIR__ .'/../uploads/media/YY/mm';으로 이동해야합니다. 여기

내 기능 :

function grab_pictures() { 
    $mpath = __DIR__ .'/../uploads/media'; 

    foreach (glob("$mpath/*") as $file) { 
     //find timestamp of last modified time 
     $lastmoddate = filemtime($file); 
     //grab only the filename with extension for newPath 
     $basename = basename($file); 
     //takes month from timestamp 
     $month = date("m", filemtime($lastmoddate)); 
     //takes Year from timestamp 
     $year = date("Y", filemtime($lastmoddate)); 
     //creates new folders /Year/month/ based on last modified time of each file 
     $newPath = __DIR__ .'/../uploads/media/' .$year. '/' .$month. '/'; 

     if (!is_dir($newPath)) { 
      mkdir($newPath, 0775, true); 
     } 

     $newName = '/' .$year. '/' .$month. '/' .$basename; 

     //change path in MySQL 
     $this->db->query(sprintf("UPDATE `images` SET `path` = '%s', `time` = 'time'", $newName)); 

     // Move files from old to the new path 
     move_uploaded_file($basename, $newPath); 
    } 

} 

내 기능의 문제점은 무엇입니까? 어떤 파일이 이동되지 않았 단 하나의 폴더 (1970년에서 1901년까지)

솔루션을 개발했다

은 아마 다른 더 좋은 방법이 있지만 여기 내 솔루션입니다 :

function grab_pictures() { 
    $mpath = __DIR__ .'/../uploads/media'; 

    foreach (glob("$mpath/*") as $file) { 
     $lastmoddate = filemtime($file); 
     $basename = basename($file);  
     $month = date("m", $lastmoddate); 
     $year = date("Y", $lastmoddate); 
     $newPath = __DIR__ .'/../uploads/media/' .$year. '/' .$month. '/'; 

     if (!is_dir($newPath)) { 
      mkdir($newPath, 0775, true); 
     } 

     $newName = '/' .$year. '/' .$month. '/' .$basename; 

     $old_Path = $mpath. '/' .$basename; 
     $new_Path = $mpath.$newName; 

     $this->db->query(sprintf("UPDATE `images` SET `path` = '%s', `time` = `time` WHERE `art` = '%s'", $newName,$basename)); 

     // Move the file 
     rename($old_Path, $new_Path); 
    } 

} 

답변

1
//takes month from timestamp 
$month = date("m", filemtime($lastmoddate)); 
//takes Year from timestamp 
$year = date("Y", filemtime($lastmoddate)); 

이유는 타임 스탬프에 filemtime을 사용하셨습니까? 대신 무엇을해야 그것은 $ mpath``내부 폴더의 하위 폴더를 만들고, 나는 단지 하나의 나쁜 일이 여기에 본 ...

//takes month from timestamp 
$month = date("m", $lastmoddate); 
//takes Year from timestamp 
$year = date("Y", $lastmoddate); 

을 그리고 당신이 정말로 유용 대신 move_uploaded_file

+0

감사의 rename를 사용해야합니다 이 시도 이 파일에 대해서만, 내가 할 수있는 일이 있습니까? (어쨌든 나는 당신의 대답을 받아 들인다) – ilvalentino4ever

+0

foreach의 시작 부분에 다음과 같이 덧붙이려고 시도한다 : 'if (is_dir ($ file)) 계속 진행 ' – krzmig

+0

'$ foreach (glob ("$ mpath/*")) {if (is_dir ($ file)) {...}}'맞습니까? – ilvalentino4ever

관련 문제