0

ホストに /clientupload/ というフォルダー名があります。clientupload フォルダーとそのサブフォルダー内のファイル数を合計 200 に制限したいと考えています。

私はそれを行う方法がわかりません!

4

2 に答える 2

3

ユーザーにファイルをアップロードさせる前に、フォルダー内のファイルの量を(php経由で)確認できます

ive は、これをサブフォルダーで動作するように変更します。

たとえば(これを実行しなかったので少し変更する必要があるかもしれません....):

<?php

  define("MAX_UPLOAD_AMOUNT", 200);
  //switch to your dir name
  $dirName = "/Temp/";
  //will count number of files
  $totalFileAmount = countFiles($dirName);

function countFiles($dirName){


    $fileAmount = 0;
  //open dir
  $dir = dir($dirName);

  //go over the dir
  while ($file = $dir->Read()){
    //check there are no .. and . in the list
    if (!(($file == "..") || ($file == "."))){
        //check if this is a dir
        if (Is_Dir($dirName . '/' . $file)){
            //yes its a dir, check for amount of files in it 
            $fileAmount += countFiles($dirName . '/' . $file);
        }
        else{
        //its not a dir, not a .. and not a . so it must be a file, update counter
        $fileAmount++;
        }
    }
  }

  return $fileAmount;
}

    //check if user can upload more files
    if ($totalFileAmount >= MAX_UPLOAD_AMOUNT)
        echo "You have reached the upload amount limit, no more uploaded";
    else
        echo "let the user upload the files, total number of files is $totalFileAmount"; 

  ?>
于 2013-04-28T06:19:40.163 に答える
0

私は自分で実用的な解決策を見つけました!以下のコードを試すことができます。変更できるファイルの上限は 200 です。

<?php

define("MAX_UPLOAD_AMOUNT", 200);

function scan_dir($path){
    $ite=new RecursiveDirectoryIterator($path);

    $bytestotal=0;
    $nbfiles=0;
    foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {

        $nbfiles++;
        $files[] = $filename;
    }

    $bytestotal=number_format($bytestotal);

    return array('total_files'=>$nbfiles,'files'=>$files);
}

$files = scan_dir('folderlinkhere');

if ($files['total_files'] >= MAX_UPLOAD_AMOUNT)
        echo "Files are more than 200.  ";
    else
             echo "Carry out the function when less than 200";
?>
于 2013-04-28T09:50:08.320 に答える