Jump to content

Reading an uploaded CSV without keeping it in file system?


cryostar
 Share

Recommended Posts

Hello!

I created an admin page that allows a batch record insert/update via uploading a csv file. I was able to read the file using the following:

protected function ___processUploadBatchRecord(InputfieldForm $form) { //called from an admin page ___executeUploadRecords
    $form->processInput($this->input->post);
    $pageFile = $form->get('order_record')->value;
    $destPath = $form->get('order_record')->destinationPath;
    $file =  fopen($destPath.$pageFile,"r");
    $headers = fgetcsv($file, 0, ',');
    $orders = array();
    while ($row = fgetcsv($file, 0, ',')){
      $orders[] = array_combine($headers, $row);
    }

	//TODO: Create a new page to store these values
}

The problem is processInput is storing the csv file in the system, which I don't want because it's a file that should be discarded after reading through it.

Is this correct? I was thinking of accessing $_FILE variable but I don't know if that's a good practice in processwire.

Link to comment
Share on other sites

I regularly use the ImportPagesCSV module and upload around 3-5 files each week.

Right now none of these files exists anymore in the filesystem. Therefore I looked into the module and it uses $files->unlink()

Here is a link to that line via Github: https://github.com/ryancramerdesign/ImportPagesCSV/blob/93fa3f1110be5b51b50c5f903d1d182a20565b26/ImportPagesCSV.module#L441

 

Maybe that helps a bit.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...