1

On a custom form I have a file field. Having chosen the file and clicked Submit, I'm trying to find how to access and read the uploaded file. I have no need to move or save it, I simply need to open it and use fgetcsv on it.

I've looked at the online examples I can find, but they seem to either deal with managed files or moving and saving unmanaged ones. The Examples module seems to only have a placeholder with regards to the file example.

0

1 Answer 1

3

Maybe have a look at the CSVimportForm.php in xurizaemon/csvimport sample module. I wrote the D8 version. There an unmanaged uploaded CSV file gets read line by line. This happens in a batch process to prevent a timeout when you process a CSV with let's say a million lines.

The file gets uploaded to the temp dir and will be added to the files table as a temporary file. We make a copy of the file to the public files dir and let the garbage collector delete the original upload. You need to put the file somewhere on the server to read it in PHP. You don't have access to the browser in PHP. You then can delete the file when you are done.

1
  • 1
    A note for anyone who wants to access an unmanaged file without moving it. Use file_save_upload, and note that if saving it in temporary://, it is there even if an ls doesn’t show it.
    – JAyenGreen
    Mar 16, 2021 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.