进入我的服务器我有一个独特的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);
}
}
}
'$ path = __DIR__。'/ ../uploads/img /'。日期(“Y”)。 '/'。日期(“m”)。 '/'。 $ filename;'*或许?* –
@ Fred-ii-谢谢,但并不那么简单。我的问题是我不知道如何对已经存在于服务器中的文件执行相同的操作。 – ilvalentino4ever
不知道你是否可以这样做... – Tonza