Solved

regex to find files not working

Posted on 2009-05-15
11
352 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP & MySQL - Rounding Results from a Select Query 3 25
paypal ipn to mysql 3 37
echo paypal data on screen 5 52
two ways encryption with php 3 28
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.

777 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