[php] Tìm kiếm file

, |

Vài đoạn code mẫu ,bạn có thể phát triển theo hướng của mình.
<?php
header('Content-type: text/html; charset=utf-8');
echo '
<form method="post">
Thư mục : <input name="thumuc" type="text" value="'.$_POST['thumuc'].'"><br/>
Điều kiện : <input name="dieukien" type="text" value="'.$_POST['dieukien'].'"><br/>
Tìm kiếm theo <select name="type">
<option value=1>Tên</option>
<option value=2>Nội dung</option>
</select> file<br/>
<input name="submit" type="submit">
</form>';
if ($_POST['submit']) {
$thumuc = $_POST['thumuc'];
$dieukien = $_POST['dieukien'];
$type = $_POST['type'];
kiemtra($thumuc,$dieukien, $type);
}
function kiemtra($thumuc,$dieukien, $type) {
if ($handle = opendir($thumuc)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$newfile = $thumuc."/".$file; //tạo đường dẫn mới cho file
//kiểm tra $newfile có là thư mục hay không ,nếu không thì tiến hành kiểm tra điều kiện
if (!is_dir($newfile)) {
if ($type == 1) $noidung = $file;
else $noidung = file_get_contents($newfile);
if (strstr($file, $dieukien))
echo "<a href='".$newfile."'>".$newfile."</a><br/>\n";
}
//ngược lại ,lặp lại thủ tục kiemtra() với đường dẫn thư mục mới
else {
kiemtra($newfile,$dieukien);
}
}
}
closedir($handle);
}
}
?>

0 comments:

Post a Comment

 
2009 portfolio site. All right reserved. Template by DryIcons.com follow me on twitter