Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

regex to find files not working

Posted on 2009-05-15
11
Medium Priority
?
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

715 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