I have made a php script that searches a whole directory with text files in it, everything works fine, except that it is case sensitive. How would i search without it being case sensitive? Here's the code i have so far:
<?php
$query = $_POST['search'];
if ($handle = opendir('posts')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<p>$entry ";
$file = file_get_contents('posts/'.$entry, FILE_USE_INCLUDE_PATH);
$check_str = strpos($file,$query);
if ($check_str == 0) {
print "not found</p>";
} else {
print "found</p>";
}
}
}
closedir($handle);
}
?>
Yeah, stripos() is what you're looking for. Here's the manual page.
You are maybe looking for strtolower , :
Related
Hey fellow programmers,
What I'm trying to accomplish is a dynamic way to source images from a directory with the use of a class image.
I'm stuck at supplying an exact string which returns a match but when trying to echo " with the returned match if the whole string is not provided then the image is linked with a partial string plus the extension.
<?php $image = new image; ?>
calling the class function with:
<?php $image -> get_image('this_string_1',''); ?>
The Image directory contents
<?php $path = $_SERVER['DOCUMENT_ROOT'].'/images/;'?>
this_string_1.png
this_string_2.png
this_string_3.png
The class:
<?php
class image{
function get_image($name,$class){
if(strlen($class)==0){ $class="default"; }
####[ Checks the type of image
global $doc_root,$html_path;
if($handle = opendir($doc_root.'/images/')){
while(false !==($value = readdir($handle))){
if($value !== '..' && $value!=='.'){
$extention;
$ext_jpeg = substr($value,-3);
if($ext_jpeg=='jpg'){
$extention=".jpg";
}
$ext_png = substr($value,-3);
if($ext_png=='png'){
$extention=".png";
}
$ext_gif = substr($value,-3);
if($ext_gif=='gif'){
$extention='.gif';
}
}
switch($extention){
case '.jpg':
if(preg_match("/$name/",$value,$result)){
foreach($result as $jpg){
echo '<img class="'.$class.'" src="'.$html_path.'/images/'.$jpg.'.jpg">';
}
}
break;
case '.png':
if(preg_match("/$name/",$value,$result)){
foreach($result as $png){
echo '<img class="'.$class.'" src="'.$html_path.'/images/'.$png.'.png">';
}
}
break;
case '.gif':
if(preg_match("/$name/",$value,$result)){
foreach($result as $gif){
echo '<img class="'.$class.'"src="'.$html_path.'/images/'.$gif.'.gif">';
}
}
break;
}
}
}
}
}
?>
I'm sure there are many ways to accomplish something like this. Any advice would be greatly appreciated.
As per Martins advice.
I have removed the if and switch statements and replaced it with "substr" in the second if statement.
<?php
class image{
function get_image($name,$class){
if(strlen($class)==0){ $class="default"; }
global $doc_root,$html_path;
if($handle = opendir($doc_root.'/images/')){
while(false !==($value = readdir($handle))){
if($value !== '..' && $value!=='.' && substr($value, 0, -4)==$name){
echo '<img src="'.$html_path.'/images/'.$value.'">';
}
}
}
}
}
?>
1) I do not see much point of your sequence of if statements to assign $extention. Why don't you just do $extention = substr($value,-4)?
2) The switch($extention) has to be inside the if($value !== '..' && $value!=='.')
3) What are you trying to achieve by the preg_match?
If you just want to match name completely, just do substr($value, 0, -4) == $name. Well this rely on extension having 3 characters, what's not perfect, but you rely on that already.
This is the file structure in my server:
htdocs/pdf/a/sub-a/file-01.pdf
htdocs/pdf/a/sub-a/file-02.pdf
htdocs/pdf/a/sub-a/file-03.pdf
htdocs/pdf/a/sub-b/file-01.pdf
htdocs/pdf/a/sub-b/file-02.pdf
htdocs/pdf/a/sub-b/file-03.pdf
htdocs/pdf/a/sub-c/file-01.pdf
htdocs/pdf/a/sub-c/file-02.pdf
htdocs/pdf/a/sub-c/file-03.pdf
and so on, well, I would like to print a list of any file and any subfolder like this:
<h3>sub-a</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>
<h3>sub-b</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>
<h3>sub-c</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>
my matter is i don't know how to get the list in php code.
anybody can help me?
Thanks
You might want to look at readdir and scandir: http://php.net/manual/en/function.readdir.php and http://www.php.net/manual/en/function.scandir.php. some great examples there too.
maybe something like this could work (you will need to tweek somewhat - original written for windows sorry):
$dir = "htdocs/pdf/a/";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dir2 = "htdocs/pdf/a/".$file."/";
echo "<h3>".ucwords(str_replace("-"," ",$file))."</h3><ul>";
if ($handle2 = opendir($dir2)) {
while (false !== ($file2 = readdir($handle2))) {
if ($file2 != "." && $file2 != "..") {
echo "<li>".$file2."</li>";
}
}
closedir($handle2);
}
echo "</ul><hr size='1'>";
}
}
closedir($handle);
}
For a project I'm working on now I have to manually add the functions to play songs like this
function song(){
mySong.src="song1.mp3";
mySong.play();
document.getElementById("p2").innerHTML="Now Playing:Song1";
}
But I want to be able to search a directory with php, and generate those functions from php, so I don't have to manually add each song. This is my test code, I know I did it wrong but I can't seem to get it right!
<?php
Header("content-type: application/x-javascript");
if ($handle = opendir('/wamp/www/songs/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo 'function $entry(){mySong.src=$entry; mySong.play();
document.getElementById("p2").innerHTML="Now Playing:" $entry;}';
echo "$entry";
}
}
closedir($handle);
}
?>
Have the JS function defined in your page, with a parameter for the song source:
function song(entry){
mySong.src=entry;
mySong.play();
document.getElementById("p2").innerHTML="Now Playing: " + entry;
}
In your PHP, you'll most likely want to echo a link to play each of the songs inside that folder, right?
Do this:
<?php
//Header("content-type: application/x-javascript");
if ($handle = opendir('/wamp/www/songs/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo '' . $entry . '<br />';
}
}
closedir($handle);
}
?>
Note that this will show the file's path name with extension, for better display you'd just have to do some string manipulation with php/js substr/substring and explode/split.
Alright, I rewrote the code to work with your given example:
JS (head)
function song(entry){
var mySong=document.getElementById("song1");
mySong.src=entry;
mySong.play();
document.getElementById("p2").innerHTML="Now Playing: " + entry;
}
PHP + HTML (body)
<?php
//Header("content-type: application/x-javascript");
$path = '/wamp/www/songs/';
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo '' . $entry . '<br />';
}
}
closedir($handle);
}
?>
<p id="p2"></p>
<audio id="song1" controls="controls">
<source src="" type="audio/mpeg" />
</audio>
This will work for mp3 files played in browsers with HTML5 support.
Note that Firefox doesn't support .mp3 playback inside <audio> tags, see: Why doesn't Firefox support the MP3 file format in <audio>
Thus this solution (with your <audio>) will give satisfying results in Chrome only (not tested on opera and safari). You may want to fallback to another solution for more compatibility.
If you want compatibility with IE and Firefox, you can try using an <embed>:
JS
function song(entry){
var mySong=document.createElement('embed');
mySong.src=entry;
mySong.width='250px';
mySong.height='60px';
document.getElementById("song1").innerHTML= '';
document.getElementById("song1").appendChild(mySong);
document.getElementById("p2").innerHTML="Now Playing: " + entry;
}
PHP + HTML
<?php
//Header("content-type: application/x-javascript");
$path = '/wamp/www/songs/';
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo '' . $entry . '<br />';
}
}
closedir($handle);
}
?>
<p id="p2"></p>
<div id="song1"></div>
This will be effectively cross-browser, but may request a plugin installation for Firefox.
you have an issue with your quotes. Single quotes do not parse variables. Also, you have some quotes in the wrong place.
<?php
Header("content-type: application/x-javascript");
if ($handle = opendir('/wamp/www/songs/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "function $entry(){mySong.src='$entry';mySong.play();document.getElementById('p2').innerHTML='Now Playing: $entry';}";
}
}
closedir($handle);
}
?>
but you will have another issue with this...your songs probably aren't going to conform to proper function names...you're going to have to rethink this "function for each song" thing...
I used the following code to display the contents of a Folder say images (Both Directories as well as Files in that folder)
<?php
$dir="images/"; // Directory where files are stored
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
$newvar1="$dir$filename";// For Hyperlink Path
?>
<p><a href="<?php echo $newvar1; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}
?>
But the PHP file shows an output with two additional Links
A link to the folder 'images' and
A Link to the 'Homepage'.
I tried to filter those two links with "filesize" function but getting some error.
How can I resolve this?
Skip current dir and parent dir with a check.
while(...) {
if($filename == ".." || $filename == ".") continue;
...
}
Modify your code to something like this:
while(($filename = readdir($dir_list)) !== false)
{
if($filename != '.' || $filename != '..') // This part to check and ignore
$newvar1="$dir$filename";// For Hyperlink Path
else
continue; //while loop will no further be processed!
//...
}
This is a more in-depth answer and I'm so fed up with such things... This is absolutely an horrible piece of code, even though Php does permit such horrible things.
Something like this is easier to read, easier to maintain, and easier to debug:
<?php
$tab = array();
$dir="images/"; // Directory to parse
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
if($filename != ".." && $filename != ".")
{
$tab[$filename] = $dir.$filename; // Hyperlink Path
}
}
closedir($dir_list);
}
/* Display separated from logic */
foreach ($tab as $filename => $hyperlink)
{
echo '<p>'.$filename.'</p>';
}
?>
More compact but a bit less easy to read:
<?php
$tab = array();
$dir="images/"; // Directory to parse
if ($dir_list = opendir($dir)) {
while(($filename = readdir($dir_list)) !== false) {
if($filename != ".." && $filename != ".") {
$tab[$filename] = $dir.$filename; // Hyperlink Path
}
}
closedir($dir_list);
}
/* Display separated from logic */
foreach ($tab as $filename => $hyperlink) {
echo '<p>'.$filename.'</p>';
}
?>
And some people (including my old teachers) tell that "if there's one line of code, don't use {}" (which I strongly disagree because it may lead to errors later on) but here's the "optimized" version:
<?php
$tab = array();
$dir="images/";
if ($dir_list = opendir($dir)) {
while(($filename = readdir($dir_list)) !== false)
if($filename != ".." && $filename != ".")
$tab[$filename] = $dir.$filename; // Hyperlink Path
closedir($dir_list);
}
foreach ($tab as $filename => $hyperlink)
echo '<p>'.$filename.'</p>';
?>
I have this code:
<?php
$allowed = array('file1', 'file2', 'file3');
if (in_array($_GET["url"], $allowed)) {
// You can include
} else {
// Error message and dont include
}
?>
But instead of writing all the filenames in the array, how can i do so that the files in for example my folder FILES/ is accepted an no other files. How to do that?
Use the file_exists function like this:
if (file_exists('FILES/'.basename($_GET["url"]))) {
// You can include
} else {
// Error message and dont include
}
Function to retrieve files in a folder:
<?
function fGetFilesInFolder($sFolder) {
$aFiles = array();
if(file_exists($sFolder)) {
if ($handle = opendir($sFolder)) {
while (false !== ($sFile = readdir($handle))) {
if ($sFile != "." && $sFile != "..") $aFiles[] = $sFile;
}
closedir($handle);
}
}
return $aFiles;
}
?>