• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Working with files in PHP

phaxmohdem

Golden Member
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
 
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.
 
<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.
 
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.
 
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?
 
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?
 
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 😉
 
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.
 
Back
Top