Jump to content

Need Help Adding a file size filter to this script in KB


kinitex

Recommended Posts

Can someone help me add a filter to my script that reads txt files from subfolders, I want it to only read txt files that are <= a certain KB size     

 

1.<?php 
2.set_time_limit(0); // set unlimited execution time 
3. 
4. 
5.$base_folder = $_POST['base_folder'];
6.$article_to_capture = (int)$_POST['article_to_capture'];
7. 
8.$words = explode(',', $_POST['words']);
9. 
10. 
11.// print_r($words); die(''); 
12. 
13. 
14.if(!is_dir($base_folder))
15.        die('Invalid base folder. Please go <a href="step1.php"><strong>back</strong></a> and enter correct folder.');
16. 
17.?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
18.<html>
19.<head>
20.<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
21.<title>Artcle Scraper Step 2</title>
22.<style type="text/css">
23.<!--
24.body {
25.        font-family: Verdana, Arial, Helvetica, sans-serif;
26.        font-size: 12px;
27.        color: #333333;
28.}
29.-->
30.</style>
31.</head>
32. 
33.<body>
34.<h2>Step 2 : Processing the content of the folder. </h2>
35.<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
36.  <tr bgcolor="#FFFFFF">
37.    <th width="10%"> </td>
38.    <th width="30%">BASE FOLDER NAME</td>
39.    <th width="50%"> <?php echo $base_folder;?></td>
40.    <th width="10%"> </td>
41.  </tr>
42.<?php 
43. 
44.$subfolder_arr = scandir($base_folder);
45. 
46.//print_r($arr1);
47. 
48.$total_subfolders = sizeof($subfolder_arr);
49.$subfolder_count = 0;
50.$file_count = 0;
51.$report = "";
52. 
53.$fp = fopen('articles.csv', 'w+');
54. 
55.for($i=0; $i< $total_subfolders; $i++){
56.  $file_name = $subfolder_arr[$i];
57. 
58.  if($file_name=='.'||$file_name=='..')
59.    continue;
60. 
61.  $sub_folder_name = $base_folder ."\\". $file_name;
62.  $file_type = is_dir($sub_folder_name) ? 'dir' : 'file';
63.  if($file_type=='dir'){
64.        $sub_folder_count++;
65.        $rpeort .= "Processing folder $sub_folder_count $sub_folder_name \r\n";
66.        $msg = "Processing folder $sub_folder_count $sub_folder_name \r\n";
67.?>  
68.  <tr bgcolor="#FFFFFF"><td> </td><td colspan="2">
69.        <?php echo $msg;?>
70.        </td><td> </td></tr>
71.        <tr bgcolor="#FFFFFF"><td> </td><td colspan="2">
72.        <table width="90%" cellpadding="0" cellspacing="0" border="1" bordercolorlight="#0000FF">
73.<?php
74.        // process sub folder
75.        $column1 = $file_name;
76.        $column2 = '{';
77.        $column3 = '{';
78.        $first = true;
79.        $files_arr = scandir($sub_folder_name);
80.        $article_processed =0;  // article_processed in current sub folder
81.        foreach($files_arr as $key=>$val){ 
82.            
83.          if(is_file($sub_folder_name.'\\'.$val) )
84.          { if( substr($val,-4)=='.txt' )
85.            { 
86.                  $article_processed++;
87.                  
88.                  if($article_to_capture==0 || $article_processed <= $article_to_capture ){     
89.                  
90.                          if($first==true)  $first=false;
91.                          else
92.                          {     $column2 .= '|'; $column3 .= '|'; }
93.                        
94.                          // read file get title and body
95.                          $file_content  = file($sub_folder_name.'\\'.$val);
96.                          $file_title = rtrim($file_content[0]);
97. 
98.                          $file_content[0] = '';
99.                          
100.                          $file_arr_size = sizeof($file_content);
101.                          $words_arr_size = sizeof($words);
102.                          $t=1;
103.                          while($t < $file_arr_size){
104.                                $file_content[$t] = rtrim($file_content[$t]);
105.                                //echo $file_content[$t];
106.                                //die('inside');
107.                                if( $words_arr_size>0 ){
108.                                        //die('inside');
109.                                        $temp = str_replace($words, "", $file_content[$t]);
110.                                        $file_content[$t] = $temp;
111.                                }       
112.                                $t++;
113.                                //if($t>=3) die('aa');
114.                          }     
115.                          $file_body = implode('',$file_content);
116.                          
117.                          $column2 .= $file_title; 
118.                          $column3 .= $file_body; 
119.                          
120.        ?>      
121.        <tr><td>                        
122.        <?php           //print_r($files_arr); 
123.                                echo $val ."\r\n";
124.        ?>  
125.        </td></tr>                      
126.        <?php
127.                        } //end if .txt
128.                } // article processed  
129.          } // end if is_file
130.    } // end foreach
131.?>      
132.</table>
133.</td><td> </td></tr>
134.<?php     $column2 .= '}';
135.          $column3 .= '}';
136.          
137.          // write to csv / excel file
138.          $erro = fputcsv ($fp, array($column1,$column2,$column3) );
139.          
140.  } //end if filetype
141.  else{
142.          
143.  }
144.}  // end for
145. 
146.fclose($fp);
147. 
148.?>  <tr bgcolor="#FFFFFF">
149.    <td> </td>
150.    <td colspan=""> File Generated. 
151.        Download it <a href="articles.csv" target="_blank">HERE</a></td>
152.    <td> </td>
153.  </tr>
154.</table>
155.</body>
156.</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.