将文件放入按上次修改时间建立的文件夹内

用到的我感兴趣的函数:copy($source,$dest),filemtime($filename)

代码如下:

 1 <?php
 2 /*
 3     按文件修改或创建时间将文件放入分类的文件夹
 4 */
 5 
 6 //test
 7 $path = 'file';
 8 createDeleteFile($path);
 9 
10 //将文件复制到按修改时间建立的文件夹,并且删除原位置的文件
11 function createDeleteFile($path)
12 {
13     $dir = dir($path);
14     //while( $file = $dir->read($dir) )
15     while( $file = $dir->read() )
16     {
17         if( ($file !== '.')&&($file !== '..') )
18         {
19             $value = $path . '/' . $file;
20             $time = fileTime($value);
21             $newPath = $time . '/' . $file;
22             if( !(file_exists($time) ) )
23             {
24                 mkdir($time);
25             }
26             moveFile($value,$newPath);
27         }
28         
29     }
30 }
31 
32 //获取文件的修改时间,并格式化时间
33 function fileTime($file)
34 {
35     return date('Y-m-d',filemtime($file));
36 }
37 
38 //移动原位置的文件到新位置,并且删除原位置的文件
39 function moveFile($path,$newPath)
40 {
41     if( copy($path,$newPath) )
42     {
43         echo $path . '移动位置成功。<br />';
44         if( unlink($path) )
45         {
46             echo $path . '原位置的文件删除成功。<br />';
47         }
48         else
49         {
50             echo $path . '原位置的文件删除失败。<br />';
51         }
52     }
53     else
54     {
55         echo $path . '移动位置失败。<br />';
56     }
57 }
原文地址:https://www.cnblogs.com/chuganghong/p/3062367.html