Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : How can I read an .XLS file's contents without saving it to the server?

Tags:

file

php

upload

I am working on a database program using PHP to keep track of the products we manage at my workplace.

For this project, I need to be able to select an .XLS file which contains new product data. New data consists of the following fields:

Type CHAR(3), Line CHAR(2), Number INT, Measure INT, Comments VARCHAR(255), Variation CHAR(1) i.e.('Y' || 'N')

These files are created in Excel, or Google Docs; I have found a wonderful excel_reader which allows me to extract the values from this file.

As this is an action which will happen routinely, as new products are created, so I do not want the file to be stored in my server directory (after a while there would be dozens!). I would rather that the file simply be read, because the import script I'm writing transfers the file's data into an array.

What I really want to happen is to have the user select the file's location (on their local computer) through an HTML form, and then have the script save that file's contents to a MySQL database without ever sending the file to the Server.

I would greatly appreciate any advice you can offer me, I'm not even sure that my plan is a valid way to handle this situation.

like image 400
Clad Avatar asked Dec 30 '22 20:12

Clad


1 Answers

It will have to be stored, at least temporarily. Delete the file after you have what you need from it (presumably after moving it out of the temp directory using move_uploaded_file, to the folder from which you will read it), then remove it using unlink.

As a last point, I would be a little worried about immediate deletion of uploaded files. What if something goes wrong with the script while the file is being parsed and data stored in the database? It would probably be a good idea to have a cron job that periodically deletes the files, to be on the safe side, instead of deleting them immediately.

like image 161
karim79 Avatar answered Jan 04 '23 15:01

karim79