Solved

regex to find files not working

Posted on 2009-05-15
11
348 Views
Last Modified: 2012-05-07
Greetings!

I am a PHP newbie trying to build a simple form app to search for files on a server. I want the user to enter a directory path (e.g. "/home/joe/docs") and the suffix of the file to look for (e.g "pdf").

This is the code for the webform:
/=====================================================/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="search.php" method="post">
            directory path to search: <input type="text" name="dir" />
            file suffix to search (eg: "doc"): <input type="text" name="filesearch" />
            <input type="submit" />
        </form>
    </body>
</html>
/=====================================================/
and the code for my search script "search.php"
/=====================================================/
<?php

$mydir = $_POST["dir"];
$mysearch = $_POST["filesearch"];

if ($dir = opendir($mydir)) {
    //List files in directory
    while (($file = readdir($dir)) !== false)
    //Ignores OS stuff.
    if ($file != "." && $file != "..") {
        if (preg_match('/[a-zA-Z0-9_]\.'.$mysearch. '$/', $file)) {
            echo "filename: " . $file . "<br />";
        }
    }
    closedir($dir);
}?>
/=====================================================/

It does not work, no errors nothing. I am sure the error is in the regex expression but I can't seem to figure out where.

What am I doing wrong? Can someone please help?

Thanks!

Al.
0
Comment
Question by:kristo5747
  • 6
  • 4
11 Comments
 

Author Comment

by:kristo5747
ID: 24398653
UPDATE: I tried using Glob()... Same result: no errors, nothing.
<?php

    

$mydir = $_POST["dir"];

$mysearch = $_POST["filesearch"];

    

if ($dir = opendir($mydir)) {

    while (($file = readdir($dir)) !== false)

    if ($file != "." && $file != "..") {

        $fileList = glob($mydir . "{*".$mysearch."}", GLOB_BRACE);

        foreach($fileList as $filename) {

            echo $filename."<br>";}

    }

    closedir($dir);

}?> 

<?php

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24398668
what is in $mysearch and $file when it does not work?
0
 

Author Comment

by:kristo5747
ID: 24398722
I capture the data entry from my form  and (not shown here) echo what was entered i.e.

    echo $mydir ."<br>";
    echo $mysearch."<br>";

Outputs ===>>>

/home/joe/docs
pdf

The weird part is that if I hardcode these values and running the same script from the command line, it works fine and gives me results.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24398742
are there any whitespace characters surrounding /home/joe/docs or pdf ?
0
 

Author Comment

by:kristo5747
ID: 24398771
No whitespace.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Assisted Solution

by:ncoo
ncoo earned 20 total points
ID: 24398794
I would change the preg_match to preg_match('/.'.$mysearch. '$/', $file)

This way filenames with any character will be listed. It could be your filenames have spaces or doubles dots so aren't being shown.
if ($dir = opendir($mydir)) {

    //List files in directory

    while (($file = readdir($dir)) !== false)

    //Ignores OS stuff.

    if ($file != "." && $file != "..") {

        if (preg_match('/.'.$mysearch. '$/', $file)) {

            echo "filename: " . $file . "<br />";

        }

    }

    closedir($dir);

}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24398801
How are you checking for extra whitespace characters?
if you hardcode these values and run the same script through the server do you get the same result as running from the command line?
0
 

Author Comment

by:kristo5747
ID: 24399156
a) The PDF files located in my "/home/joe/docs" have spaces in their names.
b) I am not checking for extra white space characters
c) if I hardcode these values and run the same script through the server , nothing happens!!!

That is bizarre!!

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 20 total points
ID: 24399746
Does the opendir succeed? does the server have permission to read that directory?
0
 

Author Comment

by:kristo5747
ID: 24399891
This is embarassing...

I modified my code, ran the script through the server and Opendir() returned FALSE.

I modified my code, ran the script from the command line, Opendir() returned TRUE and retrieved results.

Very likely, my problem must be related to my Apache settings. What settings do I need to modify for it to see my directory??


<?php

$mydir = $_POST['dir'];

$mysearch = $_POST['filesearch'];
 

echo "mydir:". $mydir .".<br>";

echo "mysearch:". $mysearch.".<br>";
 

$dir = opendir($mydir);
 

if ($dir ) {

    //List files in directory

    while (($file = readdir($dir)) !== false)

    //Ignores OS stuff.

    if ($file != "." && $file != "..") {

        if (preg_match('/.'.$mysearch. '$/', $file)) {

            echo "filename: " . $file . "<br />";

        }

    }

    closedir($dir);

} else { echo "opendir() returned FALSE!";

    exit;

}

Open in new window

0
 

Accepted Solution

by:
kristo5747 earned 0 total points
ID: 24400483
Problem had nothing to do with Apache settings.

I opened the directory for read/write access and made the files contained in this directory r/w as well.

It worked.

Thank you to all for your time and effort.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now