search without SQL (PHP)


SUBMITTED BY: Guest

DATE: March 15, 2023, 6:39 p.m.

FORMAT: PHP

SIZE: 7.6 kB

HITS: 444

  1. <?php error_reporting(0); ?>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. font-family: Arial;
  7. }
  8. .main {
  9. padding: 5px;
  10. }
  11. td {
  12. padding-left: 10px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <table class='main' width='100%'>
  18. <tr>
  19. <td>
  20. <form action="index.php" method="POST">
  21. <input type="text" placeholder="Insert a link or URL" name="link">
  22. <input type="text" placeholder="Name" name="name">
  23. <input type="text" placeholder="Category" name="category">
  24. <input type="submit" name="submit">
  25. &nbsp; <a href='index.php'>Home</a>
  26. </form>
  27. </td>
  28. <td>
  29. <div align='right'>
  30. <form action="index.php" method="POST">
  31. <input type="text" placeholder="Search" name="search">
  32. <input type="submit" value="Search">
  33. </form>
  34. </div>
  35. </td>
  36. </tr>
  37. </table>
  38. <?php
  39. $link = $_POST['link'];
  40. $name = $_POST['name'];
  41. $category = $_POST['category'];
  42. if(!$name){
  43. $name = preg_replace('/[^a-zA-Z0-9]/', ' ', $link);
  44. }
  45. if(!$category){
  46. $category = "others";
  47. }
  48. // Avoid write files in not allowed directories
  49. $name = str_replace('.', "", $name);
  50. $category = str_replace('.', "", $category);
  51. $category = strtolower($category);
  52. $no_symbol = array ('<', '>');
  53. // Avoid write javascript in the files
  54. $link = str_replace($no_symbol, "", $link);
  55. $name = str_replace($no_symbol, "", $name);
  56. // Get the last occurrence of '.' and the remaining text
  57. $lastDotIndex = strrpos($link, ".");
  58. if ($lastDotIndex) {
  59. $filetype = substr($link, $lastDotIndex + 1);
  60. } else {
  61. $filetype = "others";
  62. }
  63. $filetype = str_replace('.', "", $filetype);
  64. $filetype = strtolower($filetype);
  65. if(!file_exists("categories")){
  66. mkdir("categories");
  67. }
  68. if(!file_exists("categories/$category")){
  69. mkdir("categories/$category");
  70. }
  71. if (isset($_POST['submit'])) {
  72. if(!file_exists("categories/$category/$name")){
  73. $file = fopen("categories/$category/$name", "w");
  74. fwrite($file, $link);
  75. fclose($file);
  76. $result = "<br><a href='categories/$category/$name' target='_blank'>Sent with success!</a>";
  77. } else {
  78. $result = "<br>File already exists!";
  79. }
  80. }
  81. $start = $_GET['start'];
  82. if (!$start){$start = 0;}
  83. $search = $_POST['search'];
  84. if ($search == ""){$search = $_GET['search'];}
  85. // Avoid accessing the above directories
  86. $search = str_replace('.', "", $search);
  87. $c = 0;
  88. $limit = 20;
  89. $ini = $start * $limit;
  90. $end = $ini + $limit;
  91. $entry = 0;
  92. $search = strtolower($search);
  93. echo "<hr>$result <table width='100%'>";
  94. if ($search != "" ){
  95. $dir = 'categories';
  96. // Open the directory
  97. if ($handle = opendir($dir)) {
  98. // Loop through each subdirectory
  99. while (false !== ($subdir = readdir($handle))) {
  100. if ($subdir != "." && $subdir != ".." && is_dir($dir.'/'.$subdir)) {
  101. // Open the subdirectory
  102. if ($subhandle = opendir($dir.'/'.$subdir)) {
  103. // Loop through each file in the subdirectory
  104. while (false !== ($file = readdir($subhandle))) {
  105. $filename_written = $file;
  106. // Find the occurrence in lower or uppercase
  107. $file_l = strtolower($file);
  108. //if ($subdir == $search){echo 'ok';}
  109. // Check if the filename contains the string
  110. // If there is a category with the searched name all the files within that category will be displayed
  111. if (strpos($file_l, $search) !== false || $subdir == $search) {
  112. // Pagination
  113. if($entry >= $ini and $entry < $end){
  114. if ($file == "." || $file == ".."){continue;}
  115. // Display the filename
  116. //echo $dir.'/'.$subdir.'/'.$file . "<br>";
  117. $td_color = $entry % 2 == 0 ? '#EEE' : '#FFF';
  118. $file_path = $dir.'/'.$subdir.'/'.$file;
  119. $filesize = filesize($file_path);
  120. // Checks if is a binary file or text content
  121. if($filesize > 500){
  122. $contents = $file_path;
  123. } else {
  124. $contents = file_get_contents($file_path);
  125. }
  126. $lastDotIndex = strrpos($contents, ".");
  127. if ($lastDotIndex) {
  128. $filetype = substr($contents, $lastDotIndex + 1);
  129. }
  130. $filetype = strtolower($filetype);
  131. // Don't show characters or variables after the file extension
  132. $filetype = substr($filetype, 0, 3);
  133. echo "<tr style='background-color: $td_color;'><td><a href='$contents' target='_blank'>$filename_written</a></td><td>$subdir</td><td>$filetype</td><td><a href='comment.php?comment_file=$file' target='_blank'>Comment</a></td>";
  134. // Show case be a picture extension
  135. if($filetype == "png" || $filetype == "jpg" || $filetype == "jpeg" || $filetype == "gif"){
  136. echo "<td><div align='center'><a href='$contents' target='_blank'><img src='$contents' width='184px'></a></div></td>";
  137. }
  138. // Show a thumbnail case exists
  139. if(file_exists('thumbs/' . $subdir . '/' . $filename_written . '.jpg')){
  140. echo "<td><div align='center'><a href='thumbs/$subdir/$filename_written.jpg' target='_blank'><img src='thumbs/$subdir/$filename_written.jpg' width='184px'></a></div></td>";
  141. }
  142. $search_break++;
  143. }
  144. $entry++;
  145. // Skip the files of the directory when reached the total results
  146. if($search_break == $end){break;}
  147. }
  148. }
  149. if($search_break == $end){break;}
  150. // Close the subdirectory
  151. closedir($subhandle);
  152. }
  153. }
  154. }
  155. // Close the directory
  156. closedir($handle);
  157. }
  158. if ($entry == 0){
  159. echo "<br>Not found.";
  160. }
  161. }
  162. echo "</table>";
  163. if (!$search && !$start && !$result){
  164. echo "Hello!";
  165. }
  166. echo "<br><br><div align='center'>";
  167. if ($entry){
  168. for ($i = $start; $i < $start + 20; $i++) {
  169. echo "<a href='index.php?start=$i&search=$search'>$i </a>";
  170. }
  171. }
  172. echo "</div>";
  173. ?>

comments powered by Disqus