?
Solved

regex to find files not working

Posted on 2009-05-15
11
Medium Priority
?
366 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 85

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

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
 
LVL 15

Assisted Solution

by:ncoo
ncoo earned 80 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 85

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 85

Assisted Solution

by:ozo
ozo earned 80 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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