根据当前日期

时间:2015-09-02 13:07:07

标签: php

目前我的根文件夹中有一个名为showcase的目录。上传文件时,我想检查目录是否存在,如果不存在,请根据当前日期创建,然后将文件移动到该文件夹​​。

$dateYear = date('Y');
$dateMonth = date('M');
$dateDay = date('d');

if (!is_dir("/showcase/$dateYear/$dateMonth/$dateDay")) {
    mkdir("/showcase/$dateYear/$dateMonth/$dateDay");         
}

if (move_uploaded_file($fileTmpLoc,"/showcase/$dateYear/$dateMonth/$dateDay/$newName")){

    // stuff

}

$newName是文件的名称,例如SajdaT.jpg。这段代码对我没有任何作用。我怎样才能创造出我想要的东西呢?

e.g。

/showcase/2015/09/02如果它不存在则创建,然后像

一样将文件移动到它

/showcase/2015/09/02/SajdaT.jpg

1 个答案:

答案 0 :(得分:1)

Pass recursive attribute as true with the method.

<?php

// Desired folder structure
$structure = './dir1/dir2/dir3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}

// ...
?>