Jump to content

[SOLVED] quick and easy


Lodius2000

Recommended Posts

Will this work for ya, I use it all the time...

 

$pattern="(\.csv$)|(\.CSV$)";
$files = array();
if ($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))	{
if(eregi($pattern, $file))
// Here is your array --> $file
closedir($handle);
}
}

Link to comment
Share on other sites

$pattern="(\.csv$)|(\.CSV$)";

 

1. The slash is escaping the dot that follows it, because we want it to actually look for the dot (a dot in regex translates basically to "anything".

2. The "csv" is just the file-type (can also be .php, .txt, etc.)

3. the $ symbol tells the regex function to search at the end of the string (the string being, in this case, the full file name).

4. the | symbol says, "OR"

5. The check is done again for an uppercase file extension... later in the code, webent used the "eregi" function, which performs a case-insensitive regex search, but I guess he was just being extra-thorough.

6. The parentheses are just to group each section of the expression.

 

webent, correct me if something I said is wrong.

 

edit: and, yes, it is that easy.

Link to comment
Share on other sites

so here is my attempt, I am trying to count the number of instances of date('mjy') that appear in a folder so that I can add 1 to it and make a new file that is numbered sequentially, but use the day to separate the files for ease of archiving

 

i have it pointing to a folder that has 10 files with the digits 070208 in it and this script returns 1

 

<?php
$dirname = "blah2/";
$pattern="(\.csv$)|(\.CSV$)";
$files = array();
if ($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))	{
if(eregi($pattern, $file))
// Here is your array --> $file
closedir($handle);
}
}

$date = date('mjy');
$datepattern = "(" . $date . ")";
print $count; //for debugging
$count = count(eregi($datepattern, $file));
if ($count >= 0) {
$i = ($count+1);
}

//$i goes on to make the next file in my upload script

?>

Link to comment
Share on other sites

Take a look at the glob() function. It does what you want with very little code.

 

Here's an example taken from the above page:

<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>

 

Here's some code for doing what you want:

<?php
$count = count(glob('*' . date('mjy') . '*.*'));
echo $count .'<br>';
$count++;
?>

 

Ken

Link to comment
Share on other sites

Ken

 

I considered glob but couldnt figure out a way to make it count, the integer that matches the number of times the string appears in the array made from the file contents of the folder is what I want

 

basically

 

if there are 10 files in a folder called blah2 that contain the string 070208 as part of their filename i want $count to return 10

 

// i noticed that date('mjy') will return 07208, i have since changed it to 'mdy'

Link to comment
Share on other sites

ken

 

so the * is a wildcard, telling glob that anything (or nothing) can exist so your line

 

'*' . date('mjy') . '*.*'

means 'anything (or nothing)' the date 'anything (or nothing)'.'any-file-extention', or is the first * where the path goes?

Link to comment
Share on other sites

Yes, the "*" is a wild character which will match zero or more characters, so the pattern will match

070208.txt
something070208.somethingelse
xyz070208abc.12345

If you want to look in another directory, just put that directory and a "/" in front of the string:

<?php
$count = count(glob('testarea/*' . date('mdy') . '*.*'));
echo $count .'<br>';
$count++;
echo $count;
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.