Solved

PHP get all files in DIR

Posted on 2011-03-04
2
312 Views
Last Modified: 2012-06-21
My question stem form an old one I asked a while back.
OLD Question

In the code below I am trying to change the first line  Into code that will get all the files in a DIR
and run the rename script on each of them.

Exact file names are unknown.

Any help is appreciated.
Thank you

Selvol
<?php
$str = file_get_contents("a.php");
        preg_match('%<meta name=\"keywords\" content=\"([^"]*)"%i',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[0].$namesArray[1].$namesArray[3].".txt","w+");
        fwrite($file,$str);
        fclose($file);
?>

Open in new window

0
Comment
Question by:selvol
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
Use the glob function:

<?php

$filesInDirectory = glob("*");
foreach($filesInDirectory as $filename)
{
   echo $filename;
   // all your per-file logic goes here
}

?>

glob("*.php") returns only files ending in .php
glob("a*") returns all files starting with "a"
0
 
LVL 17

Author Comment

by:selvol
Comment Utility
Thank you for the help.  

Selvol
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now