PHP - Search Through Files

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
hey guys,

(btw sorry for spamming this section lol)

so I'm doing a search thing and, I want to search files like name is given in input.

Screenshot explanation
ZGpk5mAnSNKWlI_vxLMr_A.png




So how can I do that?

Thanks.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Well Javascript is a locally ran (on client side) meaning you will not be able to access the files using javascript. So you will have to use a 'POST' search with php to able to locally source the file names, types, etc. securely and safely.

Php function glob() would be best for this...

Here is a small example:

Code:
$usersSearch = "hello";

$files = glob("folders/*$usersSearch*.*");

if(count($files) > 0){
     foreach($files as $file){
        $info = pathinfo($file);
        echo "File found";
   }
}
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Well Javascript is a locally ran (on client side) meaning you will not be able to access the files using javascript. So you will have to use a 'POST' search with php to able to locally source the file names, types, etc. securely and safely.

Php function glob() would be best for this...

Here is a small example:

Code:
$usersSearch = "hello";

$files = glob("folders/*$usersSearch*.*");

if(count($files) > 0){
     foreach($files as $file){
        $info = pathinfo($file);
        echo "File found";
   }
}
Yeah I was not asking for javascript code xD

So I've edited code abit and,
now the code is like this :
PHP:
$where_post = $_POST['find_where']; 
$text = $_POST['text']; 

if($where_post == 'Anywhere') {
    $where = 'folders/'; 
}
elseif ($where_post == 'CMS') {
    $where = 'folders/CMS/';
}
elseif ($where_post == 'Emulator') {
    $where = 'folders/Emulator/';
}
elseif ($where_post == 'Full Packs') {
    $where = 'folders/Full Packs/';
}
elseif ($where_post == 'Furni Packs') {
    $where = 'folders/Furni Packs/';
}
elseif ($where_post == 'SQL') {
    $where = 'folders/SQL/';
}
elseif ($where_post == 'SWF') {
    $where = 'folders/SWF/';
}
$files = glob("../".$where."/*". $text ."*.*");

if(count($files[0]) > 0){
     foreach($files as $file){
        $info = pathinfo($file);
        $name = basename($files);

      ?> 
<div style="width:400px; padding:20px; border-bottom:1px solid white;">
    <div style="width:100px;height:100px; border:1px solid white; background:<?= $color; ?>" name="icon">
     <div class='tile-icon'>
    <span class='icon mif-folder mif-4x' style="padding:20px;"></span>
  </div>
</div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -100px; left: -133px;"><h3>CMS 1.zip</h3></div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -1px; left: 75px;">size</div>
 <div id="text">
    click 2 downl04d
 </div>
</div>
      <?php 
   }
}
else { 
 die('No files found.');
}
 ?>
and it returns 'no files found' but there is files like that name so..
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
PHP:
$files = glob("../".$where."/*". $text ."*.*");
contains unneeded checks.
PHP:
$files = glob("../".$where."/*". $text ."*");
is enough.

However, for debugging purposes, please add the GLOB_NOCHECK flag like so:
PHP:
$files = glob("../".$where."/*". $text ."*", GLOB_NOCHECK);

and instead of
PHP:
die('No files found.');
do
PHP:
var_dump($files);
exit;

and post the results.
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
PHP:
$files = glob("../".$where."/*". $text ."*.*");
contains unneeded checks.
PHP:
$files = glob("../".$where."/*". $text ."*");
is enough.

However, for debugging purposes, please add the GLOB_NOCHECK flag like so:
PHP:
$files = glob("../".$where."/*". $text ."*", GLOB_NOCHECK);

and instead of
PHP:
die('No files found.');
do
PHP:
var_dump($files);
exit;

and post the results.
It worked but, It only shows while there is two ( ).

PHP Code ATM:
PHP:
<?php
$files = glob("../".$where."/*". $text ."*", GLOB_NOCHECK);

if(count($files) > 0){
     foreach($files as $file){
        $info = pathinfo($file);
        $name = explode('* ',basename($files));

      ?>
<div style="width:400px; padding:20px; border-bottom:1px solid white;">
    <div style="width:100px;height:100px; border:1px solid white; background:<?= $color; ?>" name="icon">
     <div class='tile-icon'>
    <span class='icon mif-folder mif-4x' style="padding:20px;"></span>
  </div>
</div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -100px; left: -133px;"><h3><?= basename($file); ?></h3></div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -1px; left: 75px;"><?= filesize($file); ?></div>
 <div id="text">
    click 2 downl04d
 </div>
</div>
      <?php
   }
}
else {
var_dump($files);
exit;}
 ?>
 
It worked but, It only shows while there is two ( ).

PHP Code ATM:
PHP:
$files = glob("../".$where."/*". $text ."*", GLOB_NOCHECK);

if(count($files) > 0){
     foreach($files as $file){
        $info = pathinfo($file);
        $name = explode('* ',basename($files));

      ?>
<div style="width:400px; padding:20px; border-bottom:1px solid white;">
    <div style="width:100px;height:100px; border:1px solid white; background:<?= $color; ?>" name="icon">
     <div class='tile-icon'>
    <span class='icon mif-folder mif-4x' style="padding:20px;"></span>
  </div>
</div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -100px; left: -133px;"><h3><?= basename($file); ?></h3></div>
 <div style="float: right; cursor: auto; z-index: auto; position: relative; top: -1px; left: 75px;"><?= filesize($file); ?></div>
 <div id="text">
    click 2 downl04d
 </div>
</div>
      <?php
   }
}
else {
var_dump($files);
exit;}
 ?>
Also result is :
 
I still need this :)
 

Users who are viewing this thread

Top