2016-08-24 61 views
2

进入我的服务器我有一个独特的upload文件夹中有成千上万的图像,所以我想通过使用子文件夹重新整理它们,如../uploads/img/year/month/filename如何根据PHP中的上次修改日期创建文件夹?

对于所有新图像上传我使用这段代码为我的作品:

$path = __DIR__ .'/../uploads/img/' . date("Y") . '/' . date("m") . '/' . $filename; 

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

我如何能为我的所有文件在同一已经上传的文件夹,使新的目录和排序所有文件的最后修改日期?

例如,

filename | last modified date 
------ | ------ 
1.jpg | 24/02/2016 
2.jpg | 24/04/2016 
3.jpg | 24/06/2016 
4.jpg | 20/08/2016 
5.jpg | 24/08/2016 

目标:

../uploads/img/2016/02/1.jpg 
../uploads/img/2016/04/2.jpg 
../uploads/img/2016/06/3.jpg 
../uploads/img/2016/08/4.jpg 
         /5.jpg 

我尝试

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

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

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

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

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

     // Move the file into the uploaded folder 
     move_uploaded_file($basename, $newPath); 
    } 

} 

但它不工作,没有文件已经被移动,只有一个文件夹的创建(1970至01年)

解决方案

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

    foreach (glob("$mpath/*") as $file) { 
     if(!is_dir($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 `tracks` SET `art` = '%s', `time` = `time` WHERE `art` = '%s'", $newName,$basename)); 

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

} 
+1

'$ path = __DIR__。'/ ../uploads/img /'。日期(“Y”)。 '/'。日期(“m”)。 '/'。 $ filename;'*或许?* –

+0

@ Fred-ii-谢谢,但并不那么简单。我的问题是我不知道如何对已经存在于服务器中的文件执行相同的操作。 – ilvalentino4ever

+0

不知道你是否可以这样做... – Tonza

回答

1

您可以尝试在PHP中提供的filemtime(filename)功能。它返回您可以使用日期功能的时间戳
尝试echo date("m d Y", filemtime(filename))

在for循环中通过所有文件运行filemtime函数。这可能是循环的流程。

  1. 检查的最后修改日期(filemtime(文件名))
  2. 储存于一个变量(如mdate)
  3. 检查,如果一个文件夹的日期(mdate)存在
  4. 如果不是,创建一个文件夹(使用$ path变量)
  5. 将文件移动到新文件夹
  6. 对于将来上传的所有文件,也使用此循环。

查看下面的代码片段。这可能只是你想要的。

foreach (glob("$parent_folder/*") as $file) { 
$lastmoddate = filemtime("$file"); 
... folder creation/checking and file moving stuff ... 
} 

我希望它有助于清除一些东西。 检查这个reference page for the usage of filemtime()

+0

为什么使用'$ parent_folder'和'$ folder'? – ilvalentino4ever

+0

请看看我的尝试,有什么不对? – ilvalentino4ever

+1

@ ilvalentino4ever在您的“我的尝试”中,您使用的是'date(“m”,filemtime($ lastmoddate))' '$ lastmoddate是文件修改日期的时间戳。 您不需要在$ lastmoddate上再次运行filemtime。 只需使用'日期(“M”,$ LASTMODDATE)' 让我知道,如果它仍然抛出错误:) –

相关问题