...

View Full Version : CSV Upload error.....



nani_nisha06
10-08-2012, 10:45 AM
Hi Freinds,


I have created a CSV upload oprtion for my webpage and now I am writing a Php code for action to post data in to mysql.



<?php
session_start();
$host="localhost"; // Host name
$username="******"; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="new_user_verf"; // Table name
$myusername = $_SESSION['myusername']; //user who updating
$con = mysql_connect("$host","$username","$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
$fp = fopen("C:\Users\Desktop\new_user_data.csv", "r");
$length = 8096; /// have to be optional length ...
$fildDelineate = ','; /// or "|" ... declare what you need
$counter = 1; // to omission first row if it is table headers
while( !feof($fp) ) {
if( !$line = fgetcsv($fp, $length, $fildDelineate, '') && $counter) {
continue;
}
foreach($line as $key => $value){
// For example insert in 3 column (e.g. id, name, email )
$importSQL = "insert into $tbl_name values('$line[0]','$line[1]','$line[2]','$line[3]','$line[4]','$line[5]','$line[6]','$line[7]','$line[8]','$line[9]')";
mysql_query($importSQL) or die(mysql_error());
}
$counter++;
}
{
header("location:new_user.php") or die("record not inserted");
}
fclose($fp);
?>

The code above is generating below errors pls help me.




Warning: fopen(C:\Users\Desktop ew_user_data.csv) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\SAM\csv_upload.php on line 11


Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 16

Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 15


Please help me !!!!....and suggest what mistake am I doing here!!

Regards,
Nani

sunfighter
10-08-2012, 02:41 PM
A place to start
C:\Users\naveen.c\Desktop\new_user_data.csv

Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?

nani_nisha06
10-08-2012, 03:48 PM
A place to start
C:\Users\Desktop\new_user_data.csv

Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?

But in realty if the user try to upload from desktop then what I guess the issue would be the same so can you help me to fix this ???

Fou-Lu
10-08-2012, 07:20 PM
Uh, what?
If you upload, you get the path from the $_FILES superglobal. It is explicit in a chosen temp directory and the filename will be some randomness. You cannot access a remote filesystem.

The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.

The rest of the errors are because you didn't implement any type of error controlling. You simply assume that the resource is valid and opened successfully instead of checking that it did. Resource handles will return false if they fail, or a resource if they are successful. Block them in branches to verify if they are successful.

djm0219
10-08-2012, 08:29 PM
The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.

Better yet put that absolute path in single quotes and use forward slashes since it is a literal value.


$fp = fopen('C:/Users/Desktop/new_user_data.csv', 'r');

nani_nisha06
10-09-2012, 08:54 AM
Sure will change and see if it works !!!

nani_nisha06
10-09-2012, 03:52 PM
Sure will change and see if it works !!!

Still I see this error when I execute above code with single quotes


Warning: fgetcsv() [function.fgetcsv]: enclosure must be a character in C:\xampp\htdocs\SAM\csv_upload.php on line 16

Fou-Lu
10-09-2012, 04:31 PM
That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.

nani_nisha06
11-14-2012, 02:38 PM
That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.

Still I am stuck on this can any one help me ???

Fou-Lu
11-14-2012, 02:44 PM
Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.

nani_nisha06
11-15-2012, 05:54 AM
Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.

can that be a \n???

Fou-Lu
11-15-2012, 03:31 PM
can that be a \n???

Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.

nani_nisha06
11-15-2012, 07:39 PM
Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.

Fou-Lu,

I have removed this enclosure even though it doesn't work & even after assigning value it gives me same error can you help me solve this pls?

Fou-Lu
11-15-2012, 07:40 PM
Post the code and the error.

nani_nisha06
11-16-2012, 02:29 PM
Fou-Lu,

I have scussesfully coded below code for exporting CSV file in to Mysql but now I want to know how many of the lines ignored while inserting ...that should be a error message to be echoed to the user ..




<?php
session_start();
include_once "C:/xampp/htdocs/SAM/include/database.php";
$myusername = $_SESSION['myusername']; //user who updating

$file = $_FILES['up_file']['tmp_name'];//"new_user_verf.csv";
$fp = fopen($file,"r");
$length = 8096; /// have to be optional length ...
$fildDelineate = ','; /// or "|" ... declare what you need
$counter = 0; // to omission first row if it is table headers


while( !feof($fp) ) {
$value = fgetcsv($fp, $length, $fildDelineate);
/*print_r($value);*/
$date = date('Y-m-d H:m:s',strtotime($value[0]));

$importSQL = "INSERT IGNORE INTO $tbl_name0 (`Account_Create_datetime`,`Analyst`,`Acc_User_Name`,`Email_Address`,`User_Ip`)
VALUES ('".$date."','".$myusername."','".$value[1]."','".$value[2]."','".$value[3]."')";
$result= mysql_query($importSQL) or die(mysql_error());
$counter++;
}
fclose($fp);
if ($result==1){
$_SESSION['Notify']['type'] = 'Sucess Message:';
$_SESSION['Notify']['msg'] = 'Uploaded total number of->'.$counter.'<-records in to the database.\n';
header("location:/SAM/TEST/test.php") or die("record not inserted");
}
elseif($result==IGNORE){
$_SESSION['Notify']['type'] = 'Error Message:';
$_SESSION['Notify']['msg'] = 'Could not upload, number of->'.$counter.'<-records in to the database.\n';
header("location:/mym/new_user/new_user.php") or die("record not inserted");
}
else{
$_SESSION['Notify']['type'] = 'Error Message:';
$_SESSION['Notify']['msg'] = 'Some how messed up, check with your admin!!!';
header("location:/SAM/TEST/test.php") or die("record not inserted");
}
echo "Imported successfully!";
?>


Any suggestion appreciated.....

Regards,
Nani

Fou-Lu
11-16-2012, 03:06 PM
I don't believe that MySQL tracks a rejection option for an insert ignore. What you can do is pull the number that was successful after the result:


$iSuccessful += mysql_affected_rows($result);

And initialize $iSuccessful to 0 before the while loop, and you can compare the $iSuccessful to the $counter. $counter should represent the total number of records, while $iSuccessful should indicate the number inserted.
You should consider blocking the query into sets of even 10 entries at a time to save the query counts. Depending on the datasize you could potentially get away with hundreds of entries per query.

nani_nisha06
11-16-2012, 05:26 PM
I don't believe that MySQL tracks a rejection option for an insert ignore. What you can do is pull the number that was successful after the result:


$iSuccessful += mysql_affected_rows($result);

And initialize $iSuccessful to 0 before the while loop, and you can compare the $iSuccessful to the $counter. $counter should represent the total number of records, while $iSuccessful should indicate the number inserted.
You should consider blocking the query into sets of even 10 entries at a time to save the query counts. Depending on the datasize you could potentially get away with hundreds of entries per query.


Thanks will try it and will post the results...

nani_nisha06
11-17-2012, 05:02 PM
$iSuccessful += mysql_affected_rows($result);

.

Fou-Lu,

These suggestion worked like a charm with some personal customization....

Really thanks full on your support....

Regards,
Nani



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum