0

I seem to be having unclear results for this but how do you code in PHP to open a file browser dialogue window on click of a button so the user can select a csv file. I do not want to upload the csv file, I only want to read it's content. I read about fgetcsv but the sample codes seem to jump to reading each contents without the part where it selects the file first. I hope you guys can shed light.

What I need: Sample PHP code to browse a file without uploading, maybe get it's file path so I can then try using fgetcsv to read the contents of that file path.

Bonus if I'm not asking too much: How to read the csv file contents and get specific value for each rows/records.

Jay
  • 590
  • 6
  • 13
  • 29
  • 3
    PHP runs on the server, so you can't read a csv without uploading it first. – Digital Chris Aug 25 '14 at 12:13
  • 2
    PHP is a server-sided construct. You cannot use any PHP functions without uploading the file or contents. You may use Ajax and parse the file but this is similar to an upload. Why do you have a problem with an upload if I may ask? Another possibility would be to open the CSV file with a text-editor and copy/paste all the contents in a text-input / textarea and then send it via form and process it same as you would load the file but this is pretty similar to a upload and not very much of a user-friendly method... – Steini Aug 25 '14 at 12:13
  • Thanks guys, my problem with uloading is that some of my clients/users are from countries wherein upload speed is just 0.70mbps like Philippines, it takes about 1 or 2 minutes to upload a regular text file, what more if it's a CSV. Also, I could be wrong about this but uploading to the server could stack up some files in there when I only needed these files just once. I'm new to PHP and actually just a VBA programmer – Jay Aug 25 '14 at 13:11

2 Answers2

1

PHP runs on the server's machine. The CSV file is found on the client's machine. For the file to reach the server's machine, it must be uploaded.

So no, it's not possible to read a CSV file with PHP without uploading it first.

Also read:

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

PHP need the file to be uploaded to read it content. If you realy need to avoid file upload, you need to use JavaScript.

With PHP (the file can be temporarily uploaded on the server, not necessarily saved), you just need to use the native method fgetcsv($csv_file), it convert the csv file into array by line.

With JavaScript, you need to load the file with AJAX and then, parse it with your method or an existing library like PapaParse

Jordan
  • 3,776
  • 3
  • 22
  • 28