Solved

regex to find files not working

Posted on 2009-05-15
11
353 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

820 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