Current location: Hot Scripts Forums » General Community » Script Requests » PHP script that knows to read csv file


PHP script that knows to read csv file

Reply
  #1 (permalink)  
Old 08-04-07, 12:46 PM
ELV1I2's Avatar
ELV1I2 ELV1I2 is offline
Newbie Coder
 
Join Date: Jun 2007
Location: BA
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy PHP script that knows to read csv file

It's fair to say that I'm able to make script that is able to force the PHP to read CSV file, explode and insert into mySQL table but I have this situation:
A user wish to insert some data on remote web server using CSV file from his computer. Each time CSV file has different name (bill_001.txt, bill_002.txt...) and depends of application that is making CSV file.
I never knows what name will be each time when reading CSV file is demanding!? I know to make script where is in some variable name of CSV file but I would like to make script where is possible to use forms and nice Browse button for selection and another button for processing CSV file!
I tried and I still doing it but I'm unable how to get name of selected file...
here is some piece of code which I found somewhere but unable to properly use; also, how to put filter for files with extension like *.txt!?
PHP Code:

<form enctype="multipart/form-data" action="process_file.php" method="POST">

<
input type="hidden" name="MAX_FILE_SIZE" value="50000" />
Choose a file: <input name="uploadedfile" type="file" /><br />
<
input type="submit" value="Process this file" />
</
form
Thanks for your help.
__________________
There are 3 kind of enemies: my enemies, enemies of my friends and friends of my enemies.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-04-07, 01:36 PM
ELV1I2's Avatar
ELV1I2 ELV1I2 is offline
Newbie Coder
 
Join Date: Jun 2007
Location: BA
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb

Uuuh, I found the answer how to get name of file that a user selected in Browse dialog:

PHP Code:

$nsf=$_FILES['uploadedfile']['name'];  

echo 
'You selected: ' $nsf
but still I don't know how to "filter" in browse dialog only those files with one or few extensions (TXT, LST, ME..., just to avoid that user choose wrong file type?
__________________
There are 3 kind of enemies: my enemies, enemies of my friends and friends of my enemies.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-04-07, 01:53 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

if (!in_array(strtolower(end(explode('.'$nfs))), array('txt''lst')))
{
    exit(
'Invalid extension.');

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-04-07, 01:53 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a link to a javascript that allows you to check the file name extension in the form before the file is uploaded - http://willmaster.com/blog/javascrip...xtensions.html

To check file name extension or the file type the browser detected after the file is uploaded, use information from the $_FILES[...]['name'] (edit: using the code Nico posted) and $_FILES[...]['type'] elements.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-04-07, 03:59 PM
ELV1I2's Avatar
ELV1I2 ELV1I2 is offline
Newbie Coder
 
Join Date: Jun 2007
Location: BA
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
PHP Code:

if (!in_array(strtolower(end(explode('.'$nfs))), array('txt''lst')))
{
    exit(
'Invalid extension.');


Thanks Nico, I couldn't never remember such way of coding. Your solution is not quite what I thought but it's usefully. I adopted it. Just a small error in your code (name of variable):

PHP Code:

if (!in_array(strtolower(end(explode('.'$nsf))), array('txt''lst')))
{
    exit(
'Invalid extension.');

Thanks mab, I'll check also JavaScript solution...
__________________
There are 3 kind of enemies: my enemies, enemies of my friends and friends of my enemies.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-05-07, 04:19 AM
ELV1I2's Avatar
ELV1I2 ELV1I2 is offline
Newbie Coder
 
Join Date: Jun 2007
Location: BA
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
X_X

I made it and script is working fine on my computer in local environment but problem is that php script is working fine if CSV file is inside virtual folder where scripts are! If I try to pick a one CSV file from different position on my local disk I got error that can't find file!? It seems to me that I must at first to upload CSV file into folder of web server on Web and then to read from it..? As I'm using free php & MySQL server on Web it's impossible and uncomfortable...
I thought that this thing is simple: pick a file, detect his name, read it, explode into fields and insert fields into table of DB
But it looks no more so simply. What now?
__________________
There are 3 kind of enemies: my enemies, enemies of my friends and friends of my enemies.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
Simple php script. Write fields to text file...append each time. WillR Script Requests 3 01-22-07 03:53 PM
trying to create my first simple (?) php script, need guidance HotShotPhoto PHP 1 09-06-04 09:12 PM
Run PHP Script Within PHP Script Reg PHP 1 06-29-04 10:09 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM


All times are GMT -5. The time now is 09:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.