Using PHP get path of file on the client computer

George Fendler
George Fendler used Ask the Experts™
on
My application imports a delimited data file. The first line contains the field headings. Subsequent lines contain the data that coorisponds to the field name. The files are very large and I don't have any control over how they are created.
Module 1 has the user browse to the data file on the client computer that he wants to upload.
Module 2 Opens the file for reading and reads the first line.
The user takes several minutes (or hours) selecting which fields to import into the database. Once the selections have been made, they are posted and passed to a third module which opens the file again and reads the entire file (discarding line 1) and inserting remaining lines into a MySQL database.
I need a way to remember the client-side file spec (path and file) so that I can open it again in the third module without having the user browse to it again. This information doesn't appear to be stored in the $_FILES array.
Module 1:
<FORM ENCTYPE="multipart/form-data"   ACTION="Import.php" METHOD="POST">
	<P>File name: <INPUT TYPE="FILE" NAME="UploadFile"  />
	<INPUT TYPE='hidden' NAME='bgcolor' VALUE='<? echo $bgcolor; ?>' >
	<INPUT TYPE='submit' VALUE='Submit'>
</FORM>
 
Module 2:
$fp = fopen($fcb['tmp_name'], 'r');
$rec = fgets($fp);
$res = fclose($fp);
$fields = explode(",", $rec);
*/  A form  allows user to assign field intents and posts user choices*/
 
Module 3:
/*
Retrieves posted field assignment information, Opens the datafile for reading, and inserts the contents into the database. 
*/

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I would try this, as shown in the attached snipet..

When the file name box changes, stuff that value into another hidden input box using javascript.
<FORM ENCTYPE="multipart/form-data"   ACTION="Import.php" METHOD="POST">
	<P>File name: <INPUT TYPE="FILE" NAME="UploadFile"  onchange="document.getElementById('userfilename').value = this.value;" />
	<INPUT TYPE='hidden' NAME='bgcolor' VALUE='<? echo $bgcolor; ?>' >
	<INPUT TYPE='hidden' id='userfilename' NAME='userfilename' VALUE='' >
	<INPUT TYPE='submit' VALUE='Submit'>
</FORM>

Open in new window

George Fendlerprogrammer

Author

Commented:
Thanks Keven,
We're 99% there. This returns the file name in $_REQUEST["userfilename"] testdata.txt
I can get the file name in the $_FILES["UploadFile"] array eg:
Array
(
    [name] => testdata.txt
    [type] => text/plain
    [tmp_name] => C:\PHP\uploadtemp\php24.tmp
    [error] => 0
    [size] => 38388
)
What I really need is the path eg: c:\testfiles\testfile.txt
There is a big hole in my knowldegd of Java scripting-- namely what element ids are available to the method getElementById()
I believe that I need the complete path in order to open the file the second time.

Commented:
what it will return is only what is in the text part of the file upload item.  If the browser doesn't provide it, then we won't be able to get anymore of the file name.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

George Fendlerprogrammer

Author

Commented:
Kevin,
I am an old codger. Most of my programming has been in archaic languages like COBOL, Fortran, pl/1, and several assembly languages. Each language has it's idiosyncrasies. The common thread is they all have some way of passing data between modules. sometimes by reference and sometimes in stream. Some systems have ways to use named pipes or queues. Some even can "push" data onto a stack so the next module can "pop" it off the stack and use it that way.
Somehow, the PHP fopen() function can open the file even though the data that is visible in the passed array only includes the file name and the name/ location of the temporary file that is copied to the server.
PHP internally knows how to find the file on the client machine. That information is known to PHP at some point. What we need to find is a way to save that location so the next module that needs it can open and read the file.
Do you know of a way to access PHP's internal storage?

Commented:
Well...... I can appriecate where you come from..... I started my computer work before the PC era.  I'm I still do contract programming in COBOL, supporting some OLDDDDDD systems, along with many other lanaguages, even though that isn't our primary focus.

But... on the web... and HTTP... PHP can only work with what it gets from the browser.

Its the browser that opens the file on the clients computer.  The browser that adds what it wants to the headers of the http request (file uploads are http requests) and in the body of the request, it sends the data of the file (encoded sometimes).  The server and then PHP receives what the browser sends.  PHP receives the data and puts that data into a temporary file before it gives control to your PHP script.  The server and PHP has no other connection to the client.  It can only deal with what the browser sends.

I was attempting to gather additional information using javascript to grab the file name that the browser has on the screen after they click browse for the file.  But if you pick a file in the your current folder, then the complete folder name is probably not showing there, so therefore my fix would not pick up the complete folder name.   Here again, javascript can only pick up whats on the page, it can't pick up anything about the computer, filesystem, etc, unless the browser decides to share it.  Anything is is considered a security issue.

All that being said, the only work around to your issue is using something "else", like a java applet, to do the file upload, instead of the browser.  The applet would live in the browser, so the user wouldn't really know the difference.  The issue is, you need to create a "signed" java app, and the user must accept the additional program in their browswer, in order to get any information that is potentially insecure.  File system names, etc are potential security risks.

Hopfully this clears things up a little.
George Fendlerprogrammer

Author

Commented:
That's interesting and potentially useful. Does that mean that the browse button (or the fopen() ) is actually copying the entire monster file to my php server's temp directory? If that's the case, would it be possible to copy that file to a tempory area on a web server, fopen() and fread() it from there and then delete it when i'm done? Is there a copy function within PHP that allows this?
Commented:
When you click the browse button it chooses the file.  When you click the button that does the submit, the entire monster file is being sent from the browser to the web server, and PHP is indeed putting the entire monster file in the php servers temp directory.  Then, your script gets called.

I re-read your original question.  The solution to your problem would be to simply copy the file somewhere, and not ask for the file upload again.  You'll need to be sure that where ever you copy it to on your web server, that the server has permissions to do so.  Usually that involves giving write permissions for the user which the web server runs as (usually user 'apache' or 'nobody' for apache based servers)  to a special directory you've created for the purpose.  

Copy is available in PHP.    http://us3.php.net/manual/en/function.copy.php

Your only trick will be what to name it so you can find it again for the second request, and deciding what directory to put it into.

George Fendlerprogrammer

Author

Commented:
I work from my home office now. I like the quiet and the ability to have minimum interruptions.However, it is really nice to have other programmers around to bounce ideas off of.

Thanks for the help. The solution you provided worked well. My first solution was to avoid uploading the entire file twice. This acomplished the same thing.
Thanks,
George
George Fendlerprogrammer

Author

Commented:
Thanks. This solution solves the problem and streamlines the application.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial