创建上传功能以保存特定路径并检查文件夹

时间:2018-02-06 16:53:12

标签: php

我有一个上传文件(视频,音频,图像和文档)的功能..但我想不出如何继续它..顺便说一下,我是编程的初学者。我想上传一个文件并检查我要保存文件的文件夹的限制,如果文件夹达到内部的10 000文件的限制,如果是真的那么它将创建一个具有相同名称的新文件夹但中有一个数字(文件夹,文件夹1将是下一个创建的)并将其保存到创建的文件夹中..

 function uploads($filename,$tempname){

      $errors= array();
      $file_name = $filename;
      $file_tmp = $tempname;
      $audioPath = "./archivestorage/upload/media/audio/";
      $videoPath = "./archivestorage/upload/media/video/";
      $imagePath = "./archivestorage/upload/media/images/";
      $documentPath = "./archivestorage/upload/document/";
      $file = pathinfo($filename,PATHINFO_EXTENSION);
      if(empty($errors)==true) {



                    if($file ==='jpg')
                    {   
                         move_uploaded_file($file_tmp,$imagePath.$file_name);
                         echo "Success";
                         $return = $imagePath;


                    }
                    elseif($file ==='mp4')
                    {
                         move_uploaded_file($file_tmp,$videoPath.$file_name);
                         echo "Success";
                          $return = $imagePath;
                    }
                    elseif($file ==='mp3')
                    {
                         move_uploaded_file($file_tmp,$audioPath.$file_name);
                         echo "Success";
                          $return = $imagePath;
                    }
                    elseif($file ==='jpeg')
                    {
                         move_uploaded_file($file_tmp,$imagePath.$file_name);
                         echo "Success";
                          $return = $imagePath;
                    }
                    else if($file ==='docx')
                    {
                         move_uploaded_file($file_tmp,$documentPath.$file_name);
                         echo "Success";
                          $return = $imagePath;
                    }
                    elseif($file ==='png')
                    {
                         move_uploaded_file($file_tmp,$imagePath.$file_name);
                         echo "Success";
                          $return = $imagePath;
                      }
                   else{
                 print_r($errors);
                    }
                    }

1 个答案:

答案 0 :(得分:0)

我在这里给你简单的演示。可能根据你的需要不正确。如果您的php版本高于FilesystemIterator

,请记住课程5.3
    if($file ==='jpg')
    {  
     $folderChk = new FilesystemIterator($imagePath, FilesystemIterator::SKIP_DOTS)
        if (iterator_count($folderChk)<10000) {
            move_uploaded_file($file_tmp,$imagePath.$file_name);
            echo "Success";
            $return = $imagePath;
        }
        else{
            $imagePath=$imagePath." 1 " ;
            mkdir($imagePath, 0777, true);
            move_uploaded_file($file_tmp,$imagePath.$file_name);
            echo "Success";
            $return = $imagePath;

    }
}

如果不起作用,您可以使用glob()尝试

if($file ==='jpg')
{   $filecount = 0;
    $files = glob($imagePath . "*");
    if ($files){
     $filecount = count($files);
    }
    if ($filecount<10000) {
        move_uploaded_file($file_tmp,$imagePath.$file_name);
         echo "Success";
        $return = $imagePath;
    }
    else{
        $imagePath=$imagePath." 1 " ;
        mkdir($imagePath, 0777, true);
        move_uploaded_file($file_tmp,$imagePath.$file_name);
        echo "Success";
        $return = $imagePath;
    }



}

如果你想创建多个文件夹,你应该尝试这样

$imagePath = "./archivestorage/upload/media/images/";   
    $folderCount = 0;
    $folder = scandir($imagePath);//read directory 
        if ($folder){
            $folderCount = count($folder)-2;//count dir
        }
    $file = pathinfo($filename,PATHINFO_EXTENSION);
    if(empty($errors)==true) {
        if($file ==='jpg')
        {   
            $filecount = 0;
            if (is_dir($imagePath)) {
                $files = glob($imagePath . "*");
                if($files){
                    $filecount = count($files);
                }
                if ($filecount<10000) {
                    move_uploaded_file($file_tmp,$imagePath.$file_name);
                    echo "Success";
                    $return = $imagePath;
                }
                else
                {
                    for ($i = 1; $i <=$folderCount ; $i++) {
                        if(is_dir($imagePath." ".$i)) {
                            $imagePath=$imagePath." ".$i;
                            $files = glob($imagePath . "*");
                            if ($files){
                                $filecount = count($files);
                            }
                            if ($filecount<10000) {
                                move_uploaded_file($file_tmp,$imagePath.$file_name);
                                echo "Success";
                                $return = $imagePath;
                                exit();
                            }
                        }
                        else
                        {
                            $imagePath=$imagePath."  "$i;
                            mkdir($imagePath, 0777, true);
                            move_uploaded_file($file_tmp,$imagePath.$file_name);
                            echo "Success";
                            $return = $imagePath;
                            exit();
                        }
                    }
                }
            }
        }
    }