6 Replies - 1243 Views - Last Post: 10 September 2010 - 08:18 AM Rate Topic: -----

#1 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

array_search not working

Posted 01 September 2010 - 12:21 PM

Hey, I have this bit of PHP code that is giving me trouble. Im trying to convert a directory of PDF files to text files and then use PHP to cut out certain chunks of text and plug them into variables.

Basically my question is why do I keep getting the "Cannot find" error when I want to do my array_search? I know the array is there because I can call print_array($array) and I get the array but I cant search. Any ideas? Im stumped.

<?php 

function print_array($aArray) {
// Print a nicely formatted array representation:
  echo '<pre>';
  print_r($aArray);
  echo '</pre>';
}


if ($handle = opendir('/var/www/html/pay.mistequaygroup.com/upload')) 
{
	//echo "Directory handle: $handle\n";
    //echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) 
	{
       if ($file != "." && $file != "..") 
	   {
			$pdfcount++;
			//We convert the pdf documents into a single text file
			$command = "pdftotext /var/www/html/pay.mistequaygroup.com/upload/" . $file . " /var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt";
			
			//Execute the command above
			$output = exec($command);	
			
			$array = file("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt") or die ("Cannot find");
			//print_array($array);
			echo array_search("Bank Routing No.", $array);
        }
		
    }
	
    closedir($handle);
	
}



?>


Is This A Good Question/Topic? 0
  • +

Replies To: array_search not working

#2 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: array_search not working

Posted 01 September 2010 - 01:40 PM

Its not your array_search throwing the Cannot Find but rather your file() function. Maybe trying testing if file is readable before running the file command

is_readable

if (is_readable("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt")) {
  $array = file("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt");
  echo array_search("Bank Routing No.", $array);
} else {
  die("The file doesn't exists or it cannot be read from");
}


Was This Post Helpful? 0
  • +
  • -

#3 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 01 September 2010 - 01:57 PM

Alright so its not readable but I have 777 permissions on all the files in that directory and the folder itself. Or is this some other kind of error?
Was This Post Helpful? 0
  • +
  • -

#4 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: array_search not working

Posted 01 September 2010 - 02:05 PM

well that function checks if its exists AND is readable, so now you want to double check if the file even exists. One thing you can try doing is also echo out your $output variable, that will give you the feedback from the exec so if its something failing on that side than it may not even be creating the file in the first place.

if (file_exists("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt")) {
  echo "we found the file";
} else {
  echo "we cannot find the file";
}



these functions are always good rule of thumbs for some type of internal error checking for your code. Rather than relying on thinking its going to be there ... just a tip :)

This post has been edited by RPGonzo: 01 September 2010 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 03 September 2010 - 07:53 AM

Thanks for all your help.

we found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe cannot find the fileThe file doesn't exists or it cannot be read from


Is what I get when I put the if file_exists statement inside the loop, so the files are being crated, and they are in an array. But I cannot search it?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: array_search not working

Posted 03 September 2010 - 08:35 AM

Have you tried print_r($array); before the array_search, to see if "Bank Routing No." actually appears in the array?
Was This Post Helpful? 0
  • +
  • -

#7 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 10 September 2010 - 08:18 AM

With this code I get the message posted below.

<?php 

function print_array($aArray) {
// Print a nicely formatted array representation:
  echo '<pre>';
  print_r($aArray);
  echo '</pre>';
}


if ($handle = opendir('/var/www/html/pay.mistequaygroup.com/upload')) 
{
	//echo "Directory handle: $handle\n";
    //echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) 
	{
       if ($file != "." && $file != "..") 
	   {
			$pdfcount++;
			//We convert the pdf documents into a single text file
			$command = "pdftotext /var/www/html/pay.mistequaygroup.com/upload/" . $file . " /var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt";
			
			//Execute the command above
			//
			$output = exec($command);
			
			
			
			
			
			if (file_exists("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt")) 
				{
	  			echo "we found the file";
				} else 
				{
	  			echo "we cannot find the file";
				}
				
			$arrayy = file("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt") or die ("Cannot find file to create array");
		
		
        }
		
		
    }
	echo array_search("DATE AMOUNT", $arrayy); 
	print_r($arrayy);
    closedir($handle);
	
}

?>


we found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe found the filewe cannot find the fileCannot find file to create array


Since its running in a loop each pdf file in the dir gets converted to text. The way the code is setup now will just keep on creating the same array, correct? So I would need to do something to create dynamic names for each array, or is there a way to concatenate the array?

Since the last thing the output gives me with this code is "Cannot find file to create array", tells me that I have no array created to search in the first place.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1