Reading a directory of files
Results 1 to 5 of 5

Thread: Reading a directory of files

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    11

    Question Reading a directory of files

    I am trying to read all the files in a directory, then dump the data into the database. It reads it fine but I can't get the $data[] to store anything. Can anyone tell me what is wrong here:

    Code:
    $directory = "./temp";
    $dir = opendir($directory);
     while($curfile = readdir($dir)) { //Loops through all the files in the directory;
       
        if ($curfile!="." && $curfile != "..") { // don't need any directories, just files
          echo "$curfile<br>"; // Checker - each file name does print to the screen
          
           // problem somewhere here when trying to read the data
                $nowfile = $_FILES[csv][tmp_name];
                $handle = fopen($nowfile,"r");
                    do {
    
                        echo "$data[0]"; // do anything with the data
    
                        } while ($data = fgetcsv($handle,1000,",",'"')); // do
        } // end if             
          } // end while
    Thanks!!!

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    It appears that you're using the super-global $_FILES for no particular reason. That line should probably read like this:

    PHP Code:
    $nowfile $directory "/" $curfile
    Then your handle will actually point to a file.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    That was it! Thanks!!!!!

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,353
    Also note that unless you have some extensive processing to do on the data in the files before it gets inserted into your database, you might be able to let the database do all of the reading/importing itself. For example, MySQL has a LOAD DATA INFILE query for this purpose.

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,623
    Quote Originally Posted by bradgrafelman View Post
    For example, MySQL has a LOAD DATA INFILE query for this purpose.
    And our vBulletin installation has a "Resolve Thread" link (under thread tools) for another purpose.

    Thanks!
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •