2015-09-02 140 views
0

目前我的根文件夹中有一个名为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。这段代码对我无能为力。我怎样才能创造出我想要的东西?

例如

/showcase/2015/09/02创建,如果它不存在,然后将文件移动到它像

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

+0

每当有文件,检查权限工作的第一位。查看错误日志并查看是否存在有关权限的错误。几乎每次有人说“它什么都不做”时,实际上就是这样,它会向错误日志中吐出大量错误。 – kainaw

+0

'is_dir'和'mkdir'行看起来更像我的URL,你真的有一个名为'showcase'的文件夹在你的文件系统的根目录吗? (即你知道你正在使用绝对路径,对吗?)O_o –

+0

'PHP警告:mkdir():没有这样的文件或目录在/ home/*****/public_html/****/upload。 91行上的php – frosty

回答

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...'); 
} 

// ... 
?> 
+0

我得到'mkdir():Permission denied' - 这是在一个共享主机 – frosty

+0

耶可能是。只需检查您作为showcase目录中用户的权限。您需要拥有写入权限才能创建一个。 –

+0

没关系我得到它的工作! :) – frosty