a litle help...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

a litle help...

Post by peterhall »

hi guys. i have this code to import a few csv files in a specific folder. sometimes is one, sometimes it's 3 or 4... when i'm running this code, it shows me that there is 3 files, but just import the last one, with the date 20100907... and, at the line 99, gives me this error, when i'm trying to move the imported files to another folder...

the echo after import:

Array
(
[0] => D:/pazevedo/Tool/CSV_Files/Motorola/20100905-vendor_nsn_gsmqos-mf_motorola.csv
[1] => D:/pazevedo/Tool/CSV_Files/Motorola/20100906-vendor_nsn_gsmqos-mf_motorola.csv
[2] => D:/pazevedo/Tool/CSV_Files/Motorola/20100907-vendor_nsn_gsmqos-mf_motorola.csv
)
Found a total of 1722 records in this csv file.


the error:

<br />
<b>Warning</b>: glob() expects parameter 2 to be long, string given in <b>C:\wamp\www\teste2.php</b> on line <b>99</b><br />
<br />
<b>Warning</b>: rename() expects at least 2 parameters, 1 given in <b>C:\wamp\www\teste2.php</b> on line <b>99</b><br />




can someone check it and tell me what's wrong?

the code:

Code: Select all

~<?php  
$databasehost = "localhost";  
$databasename = "nsn_tool";  
$databasetable = "temp_mf";  
$databaseusername ="root";  
$databasepassword = "";  
$fieldseparator = ",";  
$lineseparator = "\n";

header('Content-Type: text/plain');
define('CSV_SEARCH_PATTERN', 'D:/pazevedo/Tool/CSV_Files/Motorola/*-vendor_nsn_gsmqos-mf_motorola.csv');
define('CSV_LINE_LENGTH', 1024);
define('CSV_DELIMITER', ',');
define('CSV_ENCLOSURE', '"');
//define('CSV_ESCAPE', '\\'); // PHP 5.3.0

$files = glob(CSV_SEARCH_PATTERN);
print_r($files);


foreach ($files as $csvfile) {
    if ($fh = fopen($csvfile, 'r')) {
        while ($row = fgetcsv($fh, CSV_LINE_LENGTH, CSV_DELIMITER, CSV_ENCLOSURE/*, CSV_ESCAPE*/)) {
         
        }        
       
    }
}


$addauto = 0;  
$save = 0;  
$outputfile = "output.sql";  
if(!file_exists($csvfile)) {            
echo "File not found. Make sure you specified the correct path.\n";       
exit;   
}   
$file = fopen($csvfile,"r");   
if(!$file) {       
echo "Error opening data file.\n";       
exit;   
}   
$size = filesize($csvfile);   
if(!$size) {       
echo "File is empty.\n";       
exit;   
}   
$csvcontent = fread($file,$size);   
fclose($file);    
$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());   
@mysql_select_db($databasename) or die(mysql_error());   
$lines = 0;   
$queries = "";   
$linearray = array();     
foreach(split($lineseparator,$csvcontent) as $line) {       
$lines++;       
$line = trim($line," \t");       
$line = str_replace("\r","",$line);       
$line = str_replace("'","\'",$line);       
$linearray = explode($fieldseparator,$line);       
$linemysql = implode("','",$linearray);       
if($addauto)           
$query = "insert into $databasetable values('','$linemysql');";       
else          
$query = "insert into $databasetable values('$linemysql');";       
$queries .= $query . "\n";       
@mysql_query($query);   
}   
@mysql_close($con);   
if($save) {               
if(!is_writable($outputfile)) {           
echo "File is not writable, check permissions.\n";       
}       
else {           
$file2 = fopen($outputfile,"w");           
if(!$file2) {               
echo "Error writing to the output file.\n";           
}           
else {               
fwrite($file2,$queries);               
fclose($file2);           
}       
}   
}   

echo "Found a total of $lines records in this csv file.\n"; 

// step 1.2
// moves the imported file to the backup folder

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("nsn_tool", $con);

rename(glob("D:/pazevedo/Tool/CSV_Files/Motorola/*-vendor_nsn_gsmqos-mf_motorola.csv", "D:/pazevedo/Tool/CSV_Files/Imported_Files/*-vendor_nsn_gsmqos-mf_motorola.csv"));



// step 1.3
// delete the first line of the file "header"


mysql_select_db("nsn_tool", $con);

$query = "DELETE FROM `temp_mf` WHERE CI='$_GET[0]'";  

$deleta = mysql_query($query) or print(mysql_error());
?> 
tku !
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

Re: a litle help...

Post by peterhall »

the first part, i resolve it. just changing the "}" that belongs to the beginning of the loop to the end... makes all sense... newbie...lol...

now, it's only the move files to other folder error...


if someone can give a hand...

tku
Post Reply