dousi6087 2018-12-22 16:06
浏览 42
已采纳

代码无法在同一天上传多个文件,无法创建下一天上传和下个月

I would like to automatically get the year, month and day. With the following information i would like to create a folder for the daily uploads.

$videoFolder = $_SERVER['DOCUMENT_ROOT']."/Videos/"; 
if (!empty($_FILES)) {
    $date = new DateTime();
    $tempFile = $_FILES['file']['tmp_name']; 
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $targetFile = $date->getTimestamp() . "." . $extension;
    $year = date("Y");
    $month = date("M");
    $day = date("d");
if (is_dir($videoFolder . $year . "/" . $month . "/" . $day)) {
    move_uploaded_file($tempFile, $dayFolder.$targetFile);
    print $targetFile;
}

if (!is_dir($videoFolder.$year)) {
    mkdir( $videoFolder.$year );
    $yearFolder = $videoFolder.$year."/";
    if (!is_dir($yearFolder.$month)) {
        mkdir($yearFolder. $month);
        $monthFolder = $yearFolder . $month . "/";
        if (!is_dir($monthFolder . $day)) {
            mkdir($monthFolder.$day);
            $dayFolder = $monthFolder . $day . "/";
            move_uploaded_file($tempFile, $dayFolder.$targetFile);
            print $targetFile;
        }
    }
} 
}

EG: Today is 18/Dec/22

So if the folder does not exists create it then place the upload in that directory.

I tested it with the date set as the 23rd and no folder was created.

EG: New Years the code should automatically know and create the folder 2019 with the child folder of Jan then as Jan's child folder as 1. End Dir result; /Videos/2019/Jan/1 then Feb would be /Videos/2019/Feb/1.

It should basically increment itself.

  • 写回答

1条回答 默认 最新

  • drno94939847 2018-12-22 16:29
    关注

    I've simplified your script a bit and added realpath() to the video path just to be sure. Can you tell if this works?

    <?php
    $videoFolder = realpath($_SERVER['DOCUMENT_ROOT']) . "/Videos/";
    if (!empty($_FILES)) {
        $date = new DateTime();
        $tempFile = $_FILES['file']['tmp_name'];
        $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $targetFile = $date->getTimestamp() . "." . $extension;
        $year = date("Y");
        $month = date("M");
        $day = date("d");
    
        $dayFolder = $videoFolder . $year . "/" . $month . "/" . $day . "/";
    
        if (!is_dir($dayFolder)) {
            $mkdir_result = mkdir($dayFolder, 0775, true); // make new directory recursively
            var_dump($mkdir_result);
            if ($mkdir_result) {
                move_uploaded_file($tempFile, $dayFolder . $targetFile);
                print $targetFile;
            }
        } else {
            move_uploaded_file($tempFile, $dayFolder . $targetFile);
            print $targetFile;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 扫描枪扫条形码出现问题
  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?