[PHP] Reading all of the files in a dir into a script?

Associate
Joined
13 Nov 2003
Posts
1,567
Location
Manchester
Hi All

I am working on a script that needs to read all the CSV files in a particular folder then proccess the information in them.

The script needs to just loop for each file.

I can do the rest of the script but I am not sure how to get the files into the script and run a loop for each one

Any help would be appreciated

Thanks
Aaron
 
No, not yet. I havent got a clue how to do it.

If someone can nudge me gently in the direction of a php function I will give it a whirl

My main problem is that I am fairly new to PHP and I don't know what all the functions are called, let alone what they do :(

Thanks
Aaron
 
Basically the files are response files that have been sent in response to orders that were sent to a supplier.

I need to read into file into a script which will feed the response into the database and generate an email if any of the orders have failed.

Once the files have been processed they will be deleted.

I am not sure of the best way to achieve this. I thought just a loop for each file that imports the data into the script and then proccesses accordingly, which I can do. It is just getting the files in there.

Storing the file names in an array sounds ideal, because then I can loop for each entry in the array?

Thanks
Aaron
 
Hey Voo

It doesnt quite work, got some issues with it.

For the second file, it seems to append the content of the first, so instead of 2 lines there are 4.

Here is the code, probably something obvious that I have overlooked

Code:
<?php require_once('../Connections/wp_sql.php');  

// Relative path
$path = "C:\Inetpub\ftproot\IsolatedUser\LocalUser\****/response";

// Open Directory
$dir_handle = @opendir($path) or die("Unable to open $path");
// Run loops for each file in DIR
$row = array();
while ($file = readdir($dir_handle)) 
{
   if($file!="." && $file!="..")
   
   foreach(file('C:\Inetpub\ftproot\IsolatedUser\LocalUser\****/response/'.$file) as $line)
  {  
   $row[] = explode(',', $line);
	}
$OrderID = substr_replace($file,"",-5);
mssql_select_db('****', $conn);
$sql = "SELECT * FROM dbo.Orders where OrderID = '$OrderID'";
$result = mssql_query($sql);
while($row2 = mssql_fetch_array($result)){
$OrderID = $row2['OrderID'];
$Ordernotes = $row2['Ordernotes'];
$date_stamp = date('Y-m-d H:i:s');
$order_notes = $Ordernotes."\r\n";
$order_notes .= "**** AUTOMATED MESSAGE - ".$date_stamp." ****\r\n";
$order_notes .= "The following repsonses have been received from ****:\r\n";
}

foreach($row as $data) {
echo $data[0].'<br/>';
echo $data[1].'<br/>';
echo $data[2].'<br/>';
echo $data[3].'<br/>';
echo $data[4].'<br/>';
// Start order_notes /
if ($data[3] == '0') {
$order_notes .= "Item Code: ".$data[1]." Status: OK. Delivery Quantity: ".$data[4]."\r\n";
echo "ok";}
if ($data[3] == '1') {
$order_notes .= "Item Code: ".$data[1]." Status: REJECTED (Invalid Product) - Attention required\r\n";
echo "invalid product";}
if ($data[3] == '2') {
$order_notes .= "Item Code: ".$data[1]." Status: REJECTED (Invalid Price) - Attention required\r\n";
echo "invalid price";}
if ($data[3] == '3') {
$order_notes .= "Item Code: ".$data[1]." Status: OK - Backordered. Delivery Quantity: ".$data[4]." Consult BM for ETA \r\n";}
}
$order_notes .= "****  END ****\r\n";
mssql_select_db('****', $conn);
$updatequery = "UPDATE dbo.Orders SET Ordernotes = '$order_notes' WHERE OrderID = '$OrderID'";
$result = mssql_query($updatequery)or (mssql_error());
$order_notes = '';

}
//close the directory
closedir($dir_handle);

?>

Any ideas, I get really rubbish with the brackets etc

Aaron
 
That doesnt seem to do it, still getting 4 lines for the second file.

:(

EDIT: Ignore me, I am stupid. Fixed

Aaron
 
Last edited:
Back
Top Bottom