The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.

Search in PHP without MySQL - Guest

  • 15th March 2023 01:54:26 AM
  • PHP
  • 3652 views
  1. <?php error_reporting(0); ?>
  2.  
  3. <html>
  4.   <head>
  5.     <style>
  6.  
  7.       body {
  8.         font-family: Arial;
  9.       }
  10.                
  11.       .main {
  12.         padding: 5px;        
  13.       }        
  14.      
  15.       td {
  16.         padding-left: 10px;    
  17.       }
  18.  
  19.     </style>
  20.   </head>
  21.  
  22.   <body>
  23.     <table class='main' width='100%'>
  24.       <tr>
  25.         <td>
  26.           <form action="index.php" method="POST">
  27.             <input type="text" placeholder="Insert a link or URL" name="link">
  28.             <input type="text" placeholder="Name" name="name">
  29.             <input type="text" placeholder="Category" name="category">
  30.             <input type="submit" name="submit">
  31.             &nbsp; <a href='index.php'>Home</a>
  32.           </form>
  33.         </td>
  34.         <td>
  35.           <div align='right'>
  36.             <form action="index.php" method="POST">
  37.               <input type="text" placeholder="Search" name="search">
  38.               <input type="submit" value="Search">    
  39.             </form>
  40.           </div>
  41.         </td>
  42.       </tr>
  43.     </table>
  44.  
  45. <?php
  46.  
  47. $link = $_POST['link'];
  48. $name = $_POST['name'];
  49. $category = $_POST['category'];
  50.  
  51. if(!$name){
  52.     $name = preg_replace('/[^a-zA-Z0-9]/', ' ', $link);
  53. }
  54.  
  55. if(!$category){
  56.     $category = "others";
  57. }
  58.  
  59. // Avoid write files in not allowed directories
  60. $name = str_replace('.', "", $name);
  61. $category = str_replace('.', "", $category);
  62.  
  63. $category = strtolower($category);
  64.  
  65. $no_symbol = array ('<', '>');
  66.  
  67. // Avoid write javascript in the files
  68. $link = str_replace($no_symbol, "", $link);
  69. $name = str_replace($no_symbol, "", $name);
  70.  
  71. // Get the last occurrence of '.' and the remaining text
  72. $lastDotIndex = strrpos($link, ".");
  73.  
  74. if ($lastDotIndex) {
  75.  
  76.     $filetype = substr($link, $lastDotIndex + 1);
  77.  
  78. } else {
  79.  
  80.     $filetype = "others";
  81.  
  82. }
  83.  
  84. $filetype = str_replace('.', "", $filetype);
  85. $filetype = strtolower($filetype);
  86.  
  87. if(!file_exists("categories")){
  88.     mkdir("categories");
  89. }
  90.  
  91. if(!file_exists("categories/$category")){
  92.     mkdir("categories/$category");
  93. }
  94.  
  95. if (isset($_POST['submit'])) {
  96.     if(!file_exists("categories/$category/$name")){
  97.         $file = fopen("categories/$category/$name", "w");
  98.         fwrite($file, $link);
  99.         fclose($file);
  100.         $result = "<br><a href='categories/$category/$name' target='_blank'>Sent with success!</a>";
  101.  
  102.     } else {
  103.        
  104.         $result = "<br>File already exists!";
  105.     }
  106. }
  107.  
  108. $start = $_GET['start'];  
  109.        
  110. if (!$start){$start = 0;}
  111.  
  112. $search = $_POST['search'];
  113.  
  114. if ($search == ""){$search = $_GET['search'];} 
  115.  
  116. // Avoid accessing the above directories
  117. $search = str_replace('.', "", $search);
  118.  
  119. $c = 0;
  120. $limit = 20;
  121. $ini = $start *  $limit;
  122. $end = $ini + $limit;
  123.  
  124. $entry = 0;
  125.  
  126. if ($search == ""){$search = ' ';}     
  127.  
  128. $search = strtolower($search);
  129.  
  130. echo "<hr>$result <table width='100%'>";
  131.  
  132. if ($search != "" ){
  133.  
  134.     $dir = 'categories';
  135.  
  136.     // Open the directory
  137.     if ($handle = opendir($dir)) {
  138.  
  139.         // Loop through each subdirectory
  140.         while (false !== ($subdir = readdir($handle))) {
  141.  
  142.             if ($subdir != "." && $subdir != ".." && is_dir($dir.'/'.$subdir)) {
  143.  
  144.                 // Open the subdirectory
  145.                 if ($subhandle = opendir($dir.'/'.$subdir)) {
  146.  
  147.                     // Loop through each file in the subdirectory
  148.                     while (false !== ($file = readdir($subhandle))) {
  149.                        
  150.                         $filename_written = $file;  
  151.  
  152.                         // Find the occurrence in lower or uppercase                    
  153.                         $file_l = strtolower($file);
  154.  
  155.                         //if ($subdir == $search){echo 'ok';}
  156.                        
  157.                         // Check if the filename contains the string
  158.                         // If there is a category with the searched name all the files within that category will be displayed
  159.                         if (strpos($file_l, $search) !== false || $subdir == $search) {
  160.  
  161.                             // Pagination
  162.                             if($entry >= $ini and $entry  < $end){
  163.                                
  164.                                 if ($file == "." || $file == ".."){continue;}
  165.  
  166.                                 // Display the filename
  167.                                 //echo $dir.'/'.$subdir.'/'.$file . "<br>";
  168.  
  169.                                 $td_color = $entry % 2 == 0 ? '#EEE' : '#FFF';
  170.  
  171.                                 $file_path = $dir.'/'.$subdir.'/'.$file;
  172.  
  173.                                 $filesize = filesize($file_path);
  174.  
  175.                                 // Checks if is a binary file or text content
  176.                                 if($filesize > 500){
  177.  
  178.                                     $contents = $file_path;
  179.  
  180.                                 } else {
  181.  
  182.                                     $contents = file_get_contents($file_path);
  183.  
  184.                                 }
  185.  
  186.                                 $lastDotIndex = strrpos($contents, ".");
  187.  
  188.                                 if ($lastDotIndex) {
  189.  
  190.                                     $filetype = substr($contents, $lastDotIndex + 1);        
  191.                                 }
  192.  
  193.                                 $filetype = strtolower($filetype);
  194.  
  195.                                 // Don't show characters or variables after the file extension
  196.                                 $filetype = substr($filetype, 0, 3);
  197.  
  198.                                 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>";                  
  199.  
  200.                                 // Show case be a picture extension
  201.                                 if($filetype == "png" || $filetype == "jpg" || $filetype == "jpeg" || $filetype == "gif"){
  202.  
  203.                                    echo "<td><div align='center'><a href='$contents' target='_blank'><img src='$contents' width='184px'></a></div></td>";                  
  204.                                 }
  205.  
  206.                                 // Show a thumbnail case exists
  207.                                 if(file_exists('thumbs/' . $subdir . '/' . $filename_written . '.jpg')){
  208.  
  209.                                    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>";                  
  210.                                 }
  211.  
  212.                                 $search_break++;
  213.                            
  214.                             }
  215.  
  216.                             $entry++;
  217.                        
  218.                             // Skip the files of the directory when reached the total results
  219.                             if($search_break == $end){break;}
  220.                         }
  221.                    
  222.                     }
  223.  
  224.                     if($search_break == $end){break;}
  225.          
  226.                     // Close the subdirectory
  227.                     closedir($subhandle);
  228.                 }
  229.            
  230.             }
  231.  
  232.         }
  233.  
  234.         // Close the directory
  235.         closedir($handle);
  236.     }
  237.  
  238.     if ($entry == 0){
  239.         echo "<br>Not found.";
  240.     }
  241.  
  242. }
  243.  
  244. echo "</table>";
  245.  
  246. if (!$search && !$start && !$result){
  247.   echo "Hello!";
  248. }
  249.  
  250. echo "<br><br><div align='center'>";
  251.  
  252. if ($entry){
  253.     for ($i = $start; $i < $start + 20; $i++) {
  254.         echo "<a href='index.php?start=$i&search=$search'>$i </a>";
  255.     }
  256. }
  257.  
  258. echo "</div>";
  259.  
  260. ?>

Raw Paste
Recent Pastes
Almost finish last steps C£P£
  • 2 hours
  • 14 mins
  • 2

Worlock AC Repair
  • 10 hours
  • 18 mins
  • 23

2 AM4ZING TTEENN V1DEOS APRIL...
  • 11 hours
  • 45 mins
  • 20

Thurston County Transmission...
  • 12 hours
  • 8 mins
  • 12

T33N S3X APRIL 15 2024...
  • 2 days
  • 11 hours
  • 28 mins





About Us - Terms of Use