2013-04-28 62 views
0

我在我的主機中有一個文件夾名稱/ clientupload /。我想限制clientupload文件夾及其子文件夾中的文件數量共計200.限制文件夾中的文件數量

我沒有任何想法如何做到這一點!

+0

爲什麼要將文件夾中的文件數限制爲200? – michaelb958 2013-04-28 05:32:56

+0

我允許客戶端上傳他們的文檔,所以我想限制他們可以上傳的文件的數量。另外主要原因是Inode限制 – user1809599 2013-04-28 05:34:45

+0

除非你在Facebook的規模上構建一些東西,否則你不應該擔心inode限制。 – michaelb958 2013-04-28 05:39:09

回答

3

可以讓用戶上傳文件

香港專業教育學院改變了這一帶的子文件夾,工作前檢查(通過PHP)的文件量文件夾中。

例如(您可能需要更改有點didnt運行這個....):

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

    ?> 
+1

if($ size> = MAX_UPLOAD_AMOUNT){echo「你不能上傳更多文件」}? – michaelb958 2013-04-28 06:49:58

+0

@ user1809599注意'> =' - 如果使用'>'代替,用戶將被限制爲201個文件而不是200個。 – michaelb958 2013-04-28 06:52:36

+0

它不會計算子文件夾中的文件!不如預期好! – user1809599 2013-04-28 06:52:37

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