Re: Uploaded CSV -> database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This discussion is starting to go over my head, but fgetscv works with
uploaded files (in PHP5 anyway). Here's some sample code:

if(isset($_FILES['userfile']['tmp_name'])){
 $csvfile=fopen($_FILES['userfile']['tmp_name'],"rb");
 if($csvfile==FALSE){die('error opening file');};
 while(($aryData=fgetcsv($csvfile))!==FALSE){
//etc
}

> Actually I think fgetcsv will work with any valid file pointer and at
> least in PHP 5, the streams implementation will allow you to use a variety
> of protocols to create the stream.
>
> http://us2.php.net/manual/en/wrappers.php
>
> I understand that it isn't even too teribbly difficult to implement your
> own stream if one isn't already to your liking, but I'm afraid I haven't
> found need to go beyond the simple read-a-file-from disk style operation.
>
> Ben
>
> On Mon, 17 Oct 2005 11:45:04 -0400, Jim Moseby <JMoseby@xxxxxxxxxxxxxxxxx>
> wrote:
>
> >> -----Original Message-----
> >> From: Brian Dunning [mailto:brian@xxxxxxxxxxxxxxxx]
> >> Sent: Monday, October 17, 2005 11:39 AM
> >> To: php-general@xxxxxxxxxxxxx
> >> Subject: Re:  Uploaded CSV -> database
> >>
> >>
> >> It looks like all of those tips will easily cover me for the latter
> >> half of the operation. Any tips on how to get the uploaded CSV file
> >> into memory in order to attack it with fgetcsv()? I'd rather
> >> not ever
> >> have to actually write the file to the server's disk.
> >>
> >> Thanks!
> >>
> >
> > If you are using the "standard" file upload facilities, your file is
> > being
> > written to disk when it is being uploaded.  As far as I can tell,
> > fgetcsv()
> > will only read a file from disk:
> >
> > <?php // from the manual
> > $row = 1;
> > $handle = fopen ("test.csv","r");
> > while ($data = fgetcsv ($handle, 1000, ",")) {
> >     $num = count ($data);
> >     print "<p> $num fields in line $row: <br>\n";
> >     $row++;
> >     for ($c=0; $c < $num; $c++) {
> >         print $data[$c] . "<br>\n";
> >     }
> > }
> > fclose ($handle);
> > ?>
> >
> > If you are instead using a socket connection to receive the file in a
> > stream
> > from the client, you could assign it to a string variable, and use
> > explode().
> >
> > These are fairly uncharted territories for me, so others will likely
have
> > better answers.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux