根据当前年份和月份创建上传路径

时间:2013-10-16 11:07:36

标签: php mkdir uploading

要上传,我想检查年份文件夹和月份子文件夹是否已存在。如果他们没有,我想创建它们并保存我上传的内容。

<?php
     $newname =  $_POST['changename'];
      $temp = explode(".", $_FILES["uploadfile"]["name"]);
        $extension = end($temp);

        if(!(
        $_FILES['uploadfile']['type']=='image/jpeg' ||
        $_FILES['uploadfile']['type']=='image/png' ||
        $_FILES['uploadfile']['type']=='image/gif' ||
        $_FILES['uploadfile']['type']=='image/bmp' 
        )) // if file does not equal these types, kill it
        {
        echo  $_FILES['uploadfile']['type'] . " is not an acceptable format.";
        die();
        }

        if ($_FILES["uploadfile"]["size"] > 20000000)
            {
                echo "File too big. Max 20mb";
                die();
            }

        if ($_FILES["uploadfile"]["error"] > 0)
            {
            echo "Return Code: " . $_FILES["uploadfile"]["error"] . "<br>";
            }
          else
            {
                $new_file_name = $newname.".".$extension;
                $path = "uploads/".$new_file_name;
                move_uploaded_file($_FILES["uploadfile"]["tmp_name"],$path);
                echo json_encode(array(
                    "success" => true,
                    "imagepath" => $path,
                    "filetype" => $_FILES["uploadfile"]["type"],
                    "new_file_name" => $newname,
                    "fileName" => $_FILES["uploadfile"]["name"],
                    "fileTmp" => $_FILES["uploadfile"]["tmp_name"],                     
                ));
            }
 ?>

4 个答案:

答案 0 :(得分:4)

用这样的东西:

$year = date("Y");   
$month = date("m");   
$filename = "../".$year;   
$filename2 = "../".$year."/".$month;

if(file_exists($filename)){
    if(file_exists($filename2)==false){
        mkdir($filename2,0777);
    }
}else{
    mkdir($filename,0777);
}

您必须根据您拥有的目录结构调整此代码。它显示了检查文件或目录是否存在的基本想法,如果不存在,那么它将被创建。

编辑1:

根据需要调整代码,应该是(未经测试):

$path = "uploads/";

$year_folder = $path . date("Y");
$month_folder = $year_folder . '/' . date("m");

!file_exists($year_folder) && mkdir($year_folder , 0777);
!file_exists($month_folder) && mkdir($month_folder, 0777);

$path = $month_folder . '/' . $new_file_name;

注意:把它放在

之上
move_uploaded_file($_FILES["uploadfile"]["tmp_name"],$path);

答案 1 :(得分:3)

is_dir应该用于检查目录/文件夹是否存在file_exists,如果找到带有该文件名的文件,file_exists也将返回true。 有一个递归参数,当设置为true时将生成所有嵌套目录。 e.g。

    $pathname_parameter = date("Y") . '/' . date("m") . '/';
    $mode_parameter = 0777;
    $recursive_parameter = true; 

    if (!is_dir($pathname_parameter)) {
        mkdir($pathname_parameter, $mode_parameter, $recursive_parameter);
    }

所以你的会是这样的

    $new_file_name = $newname.".".$extension;
    $pathname = 'uploads/' . date("Y") . '/' . date("m") . '/';

    if (!is_dir($pathname)) {
        mkdir($pathname, 0777, true);
    }
    $destination = $pathname . $new_file_name;  
    /* if( (!is_file($destination)) || (isset($_POST['overwrite']) && (int)$_POST['overwrite']) ){ */
    move_uploaded_file($_FILES["uploadfile"]["tmp_name"], $destination);    
   /* } */

答案 2 :(得分:0)

if (!file_exists(date("Y")))//Checking if year folder exist
    mkdir(date("Y"));//Creating folder if it dosent exist(You may need to add ,0777) in UNIX
if (!file_exists(date("Y").'/'.date("m")))//Checking for month folder
    mkdir(date("Y").'/'.date("m"));
$path='./'.date("Y/m").'/';

答案 3 :(得分:0)

第一个参数路径示例&#34; assets / upload&#34;第二个参数允许文件&#34;选项NUll&#34;第三个参数文件名示例&#34; image.png&#34;返回文件名

public function upload_file_date_directory($paths,$allow = '', $file) {

   $year = date('Y');
   $month = date('m');
   $path = "./". $paths . $year . "/" . $month;
   if (!is_dir($path)) {
        mkdir($path, 0777, true);
   }

   $config['upload_path'] = $path;
   $config['allowed_types'] = "gif|jpg|png|jpeg|$allow";
   $config['max_size'] = '3000';
   $config['max_width'] = '2000';
   $config['max_height'] = '2000';
   $config['file_name'] = trim(str_replace(" ", "", date('dmYHis')));
   $this->load->library('upload', $config);

   $newimg = '';
   if (!$this->upload->do_upload($file)) {
        $error = array('error' => $this->upload->display_errors());
        $newimg = $error;
        return $newimg;
   } else {
        $data = array('upload_data' => $this->upload->data());
        $newimg = $data['upload_data']['file_name'];
        return  $year . "/" . $month ."/".$newimg;
   }

}