PHP beginner with questions..

I know very little about PHP… and have only used it in conjunction with Flash projects (passing data back and forth for emails text files…minor db inserts or queries…etc)

while Im sure this script is still very minor… its the most in-depth Ive done in straight PHP… =)

summary of what Im trying to do:

1.) have a php script scrape a certain/specific directory for all the (text) files in it… return/dump this into an array…

2.) use this array of all the text file names and loop through each file…opening it… searching each line for a search string/variable…recursively… each time a line is scanned/search and that search string is found in it… dump the whole line into another array…

keep doing this for all instances found in the .txt file…and until all returned .text files have been searched.

I managed to cobble together a bit of a script that actually does everything I outlined above…

to actually make this ‘app’ work and be usable for others on the lan… I created a little FLASH front-end interface so the user can pass in the:

1.) year (directory)
2.) month (sub-directory)
3.) search term (string to search all files/lines for)

because now they are just hard coded values in my php script…

however when I try to comment out the hardcoded vars and use values passed in from Flash… it doesnt do ‘anything’…

I am new to php and the rules of scope ‘really’ confuse me…

in Flash I can just define a variable outside of a function… and any reference to the variable name is the parent variable…

if I want to use/'create a var that is only used/accessible from inside the function… I declare the variable inside the function


var someVar:String = ‘blah’;

function someFunction(){
trace(someVar); // outputs blah

 var someVar:String = 'blah blah';
 trace(someVar); //outputs 'blah blah'


am I not clear on how this is supposed to work in PHP?

I declare some vars in the _root of the document (inside the <? php ? > tags of course…)

but when trying to update, increment or eveb push data into an array from inside a php function… I got errors…

I read somewhere (among others articles) that I needed to put global before any use of my ‘parent’ variables (outside function vars) but this didnt work…

I had to type

global $myVar inside every function if I wanted it to work it seems?? (which doesnt seem right?.. but this eventually got rid of my errors 1 by 1)

so my question(s) are…

1.) why can hard coding my vars work…but not when I try to send them through my flash movie… using a sendAndLoad() method on a LoadVars() object…??

2.) this crazy php scope issue? anyone have the right words for a noobie to understand? is what I did/doing the right way? how do you declare a var outside of a function and have any/all functions access/alter it without error?

here the script I cobbled together… thanks

//multi file, recursive text doc search
$searchTerm = “000103309849”;
$targetYear = “2011”;
$targetMonth = “January”;
$reportName = “itemized_report.txt”;

//$targetYear = $_POST[“targetYear”];
//$targetMonth = $_POST[“targetMonth”];
//$searchTerm = $_POST[“searchTerm”];
//$reportName = $_POST[“reportName”];

//array of all files in directory needing to be searched
$fileArray = array();
//cummulative total of all files needing to be searched (ie: $fileArray array length)
$totalFiles = 0;
//array of all records found during the file(s) search
$recordArray = array();
//cummulative total of all records returned during search (ie: $recordArray array length)
$totalRecords = 0;
//counter for current file being searched
$currentFile = 0;
//counter for the current record found in current file
$tempCount = 0;


function exportData(){
global $fileArray;
global $totalFiles;
global $recordArray;
global $totalRecords;
global $reportName;


$myFile = $reportName;
$fh = fopen($myFile, 'w') or die("can't open file");

$headerData = "Transaction Type^HAR ID#^Procedure Code^Description^Transaction Date^Payor^Transaction Amount";
//echo($headerData . "<br>");
fwrite($fh, $headerData . "\r\n");

foreach ($recordArray as $value){
	//echo($value . "<br>");
	fwrite($fh, $value);


function searchContents(){
global $searchTerm;
global $fileArray;
global $totalFiles;
global $totalRecords;
global $currentFile;
global $recordArray;
global $tempCount;
global $targetYear;
global $targetMonth;
second function called


$fp = fopen($targetYear . "/" . $targetMonth . "/" . $fileArray[$currentFile],'rb');
//$fp = fopen($fileArray[$currentFile],'rb');
//print_r("TARGET FILE : " . $fileArray[$currentFile] . "<BR><BR>");

$tempCount = 0;
while(!feof($fp)) {
	$newLine = fgets($fp);
	if($newLine === false) {
		// ... do something with $newLine
		//print_r("NEW LINE: " . $newLine . "<br>");
		//search $newLine string for search word
		if (strpos($newLine,$searchTerm)) { 
			//echo ("MATCH FOUND: "); 
			array_push($recordArray, $newLine);
			//print_r($newLine . "<br>");
			//do nothing
//print_r("<br> CURRENT FILE: " . $currentFile);
//print_r("FOUND IN CURRENT FILE: " . $tempCount);
//print_r("<br><br> TOTAL FOUND IN SEARCH: " . $totalRecords);
//print_r("<br>CUMMULATIVE TOTAL: " . count($recordArray) . "<br><br>");
//close file

if($currentFile < $totalFiles){
	//do nothing..last file searched
	print_r("<br> DONE SEARCHING ALL FILES!");
	print_r("<br> FINAL TOTAL (Array Count): " . count($recordArray) . "<br><br>");


function getFileNames(){
global $fileArray;
global $totalRecords;
global $totalFiles;
global $reportName;
global $targetYear;
global $targetMonth;

if ($handle = opendir($targetYear . "/" . $targetMonth)) {
//if ($handle = opendir('.')) {
	while (false !== ($file = readdir($handle))) {
		if($file !='getFileNames.php'  && $file != 'AC_RunActiveContent.js' && $file != 'itemizedApplication.swf' && $file != 'itemizedApplication.fla'  && $file != 'itemizedApplication.html' && $file != $reportName &&  $file !='fileSearch-2.php' && $file !='.' && $file !='..'){
			$totalFiles = count($fileArray);

//check array contents
//print_r("<br><br>Total FILES TO SEARCH: " . $totalFiles ."<br><br>");
//call next function to search file contents



thanks for any help or suggestions on what Im doing wrong.

cant seem to find the ‘edit/modify’ button??

anyways… I figured out whay passing the var(s) from Flash wasnt working…

and I was testing from the IDE… (cntrl+enter)

and the path the .php was relative…

even though the .swf/.fla were saved in the same directory in WAMP sever as the .php file…

to use a relative path…I need to test through the .html file…

to test through the IDE… I need to use http://localhost…etc…etc… to my project/script.

I’d still like some feed back on this scope stuff?

Sponsor our Newsletter | Privacy Policy | Terms of Service