Working with files in PHP

phaxmohdem

Golden Member
Aug 18, 2004
1,839
0
0
www.avxmedia.com
I"ve run into a small snag in a PHP script I'm writing. I need to be able to parse a text file that is selected by the user.

The problem I have is I can't figure out how to send the whole file path that the user selects over to my php script.

Currently it is setup so that the initial form has an element (input type="file" name="parsefile"). When the form is submitted it only returns the file name itself in my php script, not the whole file path which is needed to parse the file.

Any insights into this? I"ve done extensive Googling on the matter, but everyone just has file upload scripts, and I have yet to find help regarding opening a user-selected text file.

Thanks
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Why do you need the whole path? An input of type "file" sends the entire file. Parse that file, it doesn't matter where it used to be stored on the user's hard drive.

Even if you knew where it was stored on the user's hard drive, you still couldn't access it from the webserver.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
<input type="file"> is for uploading, not opening files on the server side.

You can't give the user any kind of nice dialogue to pick a server side file unless you put it html on your own.
 

phaxmohdem

Golden Member
Aug 18, 2004
1,839
0
0
www.avxmedia.com
Originally posted by: notfred
Why do you need the whole path? An input of type "file" sends the entire file. Parse that file, it doesn't matter where it used to be stored on the user's hard drive.

Even if you knew where it was stored on the user's hard drive, you still couldn't access it from the webserver.


So the steps I should take are:

- Upload the file to the webserver, to a specific directory
- Script reads the name of the file, and inputs that name a variable containing the file name & path to parse.
- Do the dirty work on the text file
- Output what I want for the user
- Dump the contents of the directory where the file was uploaded.

Am I missing anything there?
Thanks for the help thus far guys.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Why do you even want to have it touch disk? Can't you just work with the file in memory as it comes from the user?
 

phaxmohdem

Golden Member
Aug 18, 2004
1,839
0
0
www.avxmedia.com
Originally posted by: kamper
Why do you even want to have it touch disk? Can't you just work with the file in memory as it comes from the user?

How would I do that? When the form data is passed to the processing page, I can retrieve the file name by doing a:

var = $_POST['parsefile'];

OF the other thing I've seen around the web is using the $_FILE array doohickey (I've never used this before)

IF you were going to:

Use a form with a <input type=file> component, passed to a page that is supposed to read the contents of the selected file, and format the information in the file into custom html tables... how would you do it?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Dunno, I've only done file upload once in php before, and then I wanted it written to disk. If I were doing this, I'd be using java, where you get a nice stream to do with as you please. That's how it should be done ;)
 

phaxmohdem

Golden Member
Aug 18, 2004
1,839
0
0
www.avxmedia.com
Cool. Thanks TekniDude, I'l give that a shot.

And kamper... I have already written this app in Java and it works great. Only problem, is the only way I can execute it is through the compiler/Dev program (BlueJ), which I can do fine, but other users will not want to/know how to mess with. I'm trying to convert it into a simple php driven web page that any idiot could use after I move on from my current student job.

Its a shame, the java program I wrote is a nice Swing app, but the compiler is pretty ghetto, and I can't find a way to make my app run as a standalone program. They didn't quite get to the part in programming class yet where you actually learn how to make the program usable by other people than the developer.