Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 10:45 AM   PM User | #1
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Exclamation CSV Upload error.....

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 Code:
<?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.



Code:
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
Code:
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

Last edited by nani_nisha06; 10-08-2012 at 03:47 PM..
nani_nisha06 is offline   Reply With Quote
Old 10-08-2012, 02:41 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 362 Times in 361 Posts
sunfighter is on a distinguished road
A place to start
Code:
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?
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
nani_nisha06 (10-08-2012)
Old 10-08-2012, 03:48 PM   PM User | #3
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by sunfighter View Post
A place to start
Code:
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 ???
nani_nisha06 is offline   Reply With Quote
Old 10-08-2012, 07:20 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nani_nisha06 (10-09-2012)
Old 10-08-2012, 08:29 PM   PM User | #5
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,229
Thanks: 2
Thanked 190 Times in 188 Posts
djm0219 is on a distinguished road
Quote:
Originally Posted by Fou-Lu View Post
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.

PHP Code:
$fp             fopen('C:/Users/Desktop/new_user_data.csv''r'); 
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Users who have thanked djm0219 for this post:
nani_nisha06 (10-09-2012)
Old 10-09-2012, 08:54 AM   PM User | #6
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Sure will change and see if it works !!!
nani_nisha06 is offline   Reply With Quote
Old 10-09-2012, 03:52 PM   PM User | #7
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by nani_nisha06 View Post
Sure will change and see if it works !!!
Still I see this error when I execute above code with single quotes

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

Last edited by nani_nisha06; 10-09-2012 at 04:07 PM..
nani_nisha06 is offline   Reply With Quote
Old 10-09-2012, 04:31 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-14-2012, 02:38 PM   PM User | #9
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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 ???
nani_nisha06 is offline   Reply With Quote
Old 11-14-2012, 02:44 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 05:54 AM   PM User | #11
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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???
nani_nisha06 is offline   Reply With Quote
Old 11-15-2012, 03:31 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by nani_nisha06 View Post
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.
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 07:39 PM   PM User | #13
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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?
nani_nisha06 is offline   Reply With Quote
Old 11-15-2012, 07:40 PM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Post the code and the error.
Fou-Lu is offline   Reply With Quote
Old 11-16-2012, 02:29 PM   PM User | #15
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
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 Code:
<?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]."')";
     
$resultmysql_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
nani_nisha06 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 AM.


Advertisement
Log in to turn off these ads.