【问题标题】:Code fails to upload mulitple files on the same day and fails to create the next days upload and following month代码同一天上传多个文件失败,创建次日上传次月失败
【发布时间】:2018-12-22 16:06:27
【问题描述】:

我想自动获取年月日。有了以下信息,我想为每日上传创建一个文件夹。

$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:今天是 12 月 18 日/22 日

因此,如果该文件夹不存在,请创建它,然后将上传文件放在该目录中。

我测试了它,日期设置为 23 号,没有创建文件夹。

EG:新年代码应该会自动知道并创建文件夹 2019,然后将 Jan 的子文件夹作为 Jan 的子文件夹 1。 结束目录结果; /Videos/2019/Jan/1 然后二月将是 /Videos/2019/Feb/1。

它应该基本上自增。

【问题讨论】:

    标签: php


    【解决方案1】:

    我已经稍微简化了您的脚本,并在视频路径中添加了 realpath() 以确保安全。 你能判断这是否有效吗?

    <?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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2013-06-09
      • 2012-03-18
      相关资源
      最近更新 更多