PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #16  
Old October 25th, 2009, 12:54 PM
RandiR RandiR is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 1 RandiR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 26 sec
Reputation Power: 0
Parsing CSV files

Here is an alternate resource for parsing CSV files. Take a look at the biterscripting sample script SS_CSV. It shows how to parse CSV files. It may or may not work for your purposes.

Randi

Reply With Quote
  #17  
Old October 25th, 2009, 08:09 PM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
Hey Randi,

I'm trying to make the below part work:
PHP Code:
if(isset($_POST['submit'])){
echo 
"<table>\n";
$row 0;
$handle fopen($_POST['datafile'] ,"r");
...
... 


As mentioned earlier, I need this to happen on submit and parse the csv file I upload in the upload text box with browse button.

Sorry, I checked out the code for biterscripting sample script SS_CSV coz I didn't understand the code entirely.

Any help would be great!

Reply With Quote
  #18  
Old October 25th, 2009, 08:34 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
if its a file you are uploading, check out pretty much any file upload tutorial. they will show you to use $_FILE instead of $_POST to access the file.

Reply With Quote
  #19  
Old October 26th, 2009, 09:55 AM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
Hey,

Can't I read the uploaded file directly rather than saving it in temp location in PHP?

Probably that's what would work in my case or maybe that's what I'm trying to do so long.

Reply With Quote
  #20  
Old October 26th, 2009, 12:41 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
if it is a file you are uploading via a form (an uploaded file passed into a form using the <input type="file"> tag where you click browse and find the file locally on your computer) then you would need to use $_FILE to either read it from the temp directory or move it from the temp directory and then read the file. If it is a file that has already been uploaded by some other method like ftp or you just create a new file then you can just access the file locally by just using the relative path/name.

Reply With Quote
  #21  
Old October 26th, 2009, 01:58 PM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
The problem, I don't know what I'm missing in the code.

Totally blank what I need to fill in 'tmp_name' or 'name' parameters.

PHP Code:
<?php
//If csv file it explodes and displays the contents

if(isset($_POST['submit']){
echo 
"<table>\n";
$row 0;
if(
move_uploaded_file($_FILES['datafile']['tmp_name'], $_FILES['datafile']['name']){

$handle fopen($_FILES['datafile'],"r");
while ((
$data fgetcsv($handle1000",")) !== FALSE) {
....
.....

Reply With Quote
  #22  
Old October 26th, 2009, 05:19 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
try something like this:
PHP Code:
<?php
error_reporting
(E_ALL);
if(isset(
$_FILES) && !empty($_FILES)){
    
$name $_FILES['datafile']['name'];
    
$tmpname $_FILES['datafile']['tmp_name'];
    if(
move_uploaded_file($tmpname,$name)){
        if(
file_exists($name)){
            
$fp fopen($name"r");
            
$rowCount 0;
            while((
$data fgetcsv($fp1000",")) !== FALSE){
                
//insert into database.
                
echo "<pre>".print_r($datatrue)."</pre>";
                
$rowCount++;
            }
            echo 
"$rowCount rows were found.";
            
fclose($fp);
        } else {
            echo 
"File supposedly copied, but upon further investigation was not found.<br />check that the file exists in:&nbsp;&nbsp;&nbsp;".dirname(__FILE__)."<br />under the name:&nbsp;&nbsp;&nbsp;$name";
        }
    } else {
        echo 
"Failed to copy uploaded file. Check that the file exists in the temporary directory.<br /><br />Temp file name:&nbsp;&nbsp;&nbsp;$tmpname";
    }
    
} else {
    echo 
'
<form method="post" enctype="multipart/form-data" action="?">
<input type="file" name="datafile">
<br />
<input type="submit" value="Upload File">
</form>
'
;
}
?>

check if that works and see if you get any output. right now it just dumps the contents of the csv file to the screen so if it is a big file it might be a large screen of data. At the end it shows a count of rows found in the file also. I also confirmed this to work on my windows box.

Reply With Quote
  #23  
Old October 27th, 2009, 09:26 AM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
PHP Code:
<?php
error_reporting
(E_ALL);
if(isset(
$_FILES) && !empty($_FILES)){
$name $_FILES['datafile']['name'];
$tmpname $_FILES['datafile']['tmp_name'];
if(
move_uploaded_file($tmpname,$name)){
if(
file_exists($name)){
$fp fopen($name"r");
$rowCount 0;
while((
$data fgetcsv($fp1000",")) !== FALSE) {
echo 
"<pre>".print_r($datatrue)."</pre>";
$rowCount++;
}
echo 
"$rowCount rows are found.";
fclose($fp);
}
else
{
echo 
"File Copied, on futher investigation.<br/>check that the file exists in:&nbsp;&nbsp;&nbsp;".dirname(_FILE_)."<br />under the name:&nbsp'&nbsp;&nbsp;$name";
}
else{
echo 
"Failed to copy uploaded file.Check that the file exists in the temp dir.<br/><br/>Temp file nasme:&nbsp;&nbsp;&tmpname";
}
}

/*else
{
echo '
<form method="post" enctype="multipart/form-data" action=campaign3.php">
<input type="file" name="datafile">
<br/>
<input type="submit" value="Upload File">';
}*/

?>

this smiley tells you what I'm feeling like.

The page is blank whether I give error_reporting(E_ALL) or not.

p.s: I checked my php.ini file and error_reporting(E_ALL) is uncommented. Also I have disabled the last else part coz I have that in my page1.php.

Reply With Quote
  #24  
Old October 27th, 2009, 12:42 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
I wanted you to just try that page by itself as it is simply to see if it is working at all as that script is confirmed working on my computer. That would also let us see how far you were able to get on the page. as in "I could see the form, but nothing after submitting it". I've added a few other things just to see how far you get in the page. should help.

PHP Code:
<?php 
error_reporting
(E_ALL);
ini_set('display_errors'1);
if(isset(
$_FILES) && !empty($_FILES)){
    echo 
'$_FILES array found.<br />';
    
$name $_FILES['datafile']['name'];
    
$tmpname $_FILES['datafile']['tmp_name'];
    if(
move_uploaded_file($tmpname,$name)){ 
        echo 
'Move uploaded file worked.<br />';
        if(
file_exists($name)){ 
            echo 
'File found in script directory.<br />';
            
$fp fopen($name"r"); 
            
$rowCount 0
            while((
$data fgetcsv($fp1000",")) !== FALSE){ 
                
//insert into database. 
                
echo "<pre>".print_r($datatrue)."</pre>"
                
$rowCount++; 
            } 
            echo 
"$rowCount rows were found."
            
fclose($fp); 
        } else { 
            echo 
"File supposedly copied, but upon further investigation was not found.<br />check that the file exists in:&nbsp;&nbsp;&nbsp;".dirname(__FILE__)."<br />under the name:&nbsp;&nbsp;&nbsp;$name"
        } 
    } else { 
        echo 
"Failed to copy uploaded file. Check that the file exists in the temporary directory.<br /><br />Temp file name:&nbsp;&nbsp;&nbsp;$tmpname"
    } 
     
} else { 
    echo 

<form method="post" enctype="multipart/form-data" action="?"> 
<input type="file" name="datafile"> 
<br /> 
<input type="submit" value="Upload File"> 
</form> 
'


?>

Reply With Quote
  #25  
Old October 28th, 2009, 09:11 AM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
Hey,

It worked when I executed the code. The output for the sample csv file uploaded:

Code:
$_FILES array found.
Move uploaded file worked.
File found in script directory.

Array
(
    [0] => Data for field 1
    [1] => some data for field 2
    [2] => and a bit of data for field 3
)

Array
(
    [0] => Just some test stuff
    [1] => Mary had a little lamb
    [2] => roses are red
)

Array
(
    [0] => violets are blue
    [1] => blah blah blah
    [2] => and final field
)

3 rows were found. 


My sample csv file:
Code:
Data for field 1, some data for field 2, and a bit of data for field 3
Just some test stuff, Mary had a little lamb, roses are red
violets are blue, blah blah blah, and final field

Reply With Quote
  #26  
Old October 28th, 2009, 12:01 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
well if that worked then there is nothing wrong with your server. It is a code problem. Probably the form that submits to that page has something wrong. Double check the case in the form because PHP is case sensitive for things like field names and if needed just copy/paste the form from my code into the other page and add any other fields you need.

Reply With Quote
  #27  
Old October 28th, 2009, 12:26 PM
Naughty Naughty is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2009
Posts: 83 Naughty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 53 m 8 sec
Reputation Power: 1
Hey,

I checked the code and made it work. The problem was I didn't give name attribute in the html form for continue submit button. Once I gave the name attribute, and called in 2nd page using post option it did the job.

Now I'm trying to add checkbox dynamically to this existing table I echo out. Where I want to delete a particular row or select all rows in the table using Javascript.

Probably, I'll be posting the code in client-side things coz I need help to figure out a small thing.

Thank you!

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Parse a csv file and then store it in db


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek