Page 1 of 1

Text field that has a browse button for local file

Posted: Mon Aug 30, 2004 11:15 am
by ljCharlie
Is there a way to have a text field that has a browse button next to it that will allow me to browse my local files and select them to be inserted into the text field?

Any help is greatly appreciated!

ljCharlie

Posted: Mon Aug 30, 2004 11:20 am
by ljCharlie
Never mind, I found the file type. That works; however, is there a way to truncate the location path and only keep the file name?

Thanks!

ljCharlie

Posted: Mon Aug 30, 2004 11:24 am
by feyd
the filename should be stored in the

Code: Select all

$_FILESї'yourfileupload']ї'name']

Posted: Mon Aug 30, 2004 11:28 am
by markl999
That works; however, is there a way to truncate the location path and only keep the file name?
What feyd said .. but if you're talking on the client side then the file type input 'area' is read only and can't be written to/altered, i don't even think JS can do it as it would present a rather large security hole (being able to change/decide what files get uploaded :))

Posted: Mon Aug 30, 2004 11:29 am
by feyd
correct, Javascript doesn't have access to writing into the field either.

Posted: Mon Aug 30, 2004 11:39 am
by ljCharlie
Thank you very much. So if it is not possible, how to do trim or break the location path from the file in PHP so I can store the location path and file name to the proper field in the database? For example, I have this:

C:\MyFiles\MyWork\Website\PDF_Files\All_goes_well.pdf

I want to store the PDF_Files\ into the Location field in my MySQL database and the All_goes_well.pdf into the File Name field. How do I break this location path from the file type text field from the client side once the user hit the submit button?

ljCharlie

Posted: Mon Aug 30, 2004 11:43 am
by feyd
$_FILES['foo']['name'] only contains the filename.

if you want the location information, you'll need to use javascript to set a hidden field with the full path that was on the machine.

Posted: Mon Aug 30, 2004 1:12 pm
by ljCharlie
Okay, here's what I have on the form.

<input type="file" name="fileName">

Then on the success page:

echo "File Name: ".$_FILES['fileName']['name']."<br>";

I don't see anything echo to the screen.

ljCharlie

Posted: Mon Aug 30, 2004 1:19 pm
by markl999
Follow the examples here.
Note the <form enctype="multipart/form-data" ..... part of the form.

Posted: Mon Aug 30, 2004 1:41 pm
by ljCharlie
Many thanks for your help. That works. I didt use the $HTTP_POST_Files that's why it wasn't working. Anyway, I have another question. If I did as mentioned above, that means I just uploaded a file to my web server, correct? If that is true, then what directory or where on the server will the file be store? Basically what I want is just the path and file name and not uploading the actual file to the webs server. Will the above examples I posted cause whatever file I select to be uploaded to my webserver?

ljCharlie

Posted: Mon Aug 30, 2004 1:45 pm
by markl999
Yeah, using a file method will post the files to your server. What exactly are you trying to achieve? Get the path and name of a users local file? If so, to what end .. what do you plan to do with this information if you don't actually require the file to be uploaded?

Posted: Mon Aug 30, 2004 2:00 pm
by ljCharlie
Okay, here's what I'm trying to do. I have an event page that with certain events it links to a registration pdf file that I already uploaded to a PDF/ directory on the webserver. On the MySQL database, I have two tables. One table contains all the information about an event such as date, description, place, and so on. The second table contains information about additional files that will link to the event such as the registration pdf file. So when the user view the event, they can click on that event's link and launch the registration pdf file on their screen. I use the second table to hold this information such as the file name, location of file on the server, and the eventID that will match the actual event on the event table. So, when I insert a new event into the event table on the MySQL database, and if this event happens to require a pdf file, instead of retyping the file name and location of the pdf file, I use the browse file to select the file on my location machine. Once I have that file selected along with all the other information for that event, I submit. When submit, I want to store the file name and path to its proper field in the second table on the database. I don't actually upload any file....although that I can if I don't already uploaded the pdf file ahead of time.

The only reason why I use the browse for file is because I don't want to type the pdf file name because it's possible to make mistyped.

ljCharlie

Posted: Mon Aug 30, 2004 2:04 pm
by markl999
Well, you could always use glob or readdir (if you don't have PHP version 4.3.0 or above then you won't have glob) to get the list of existing registration pdfs (from the pdf directory on the server) and use that list to create a <select> box from which you can select the correct one to go with the event ?

Posted: Mon Aug 30, 2004 2:11 pm
by ljCharlie
Well, tha'ts a nice idea. I'll give that a try.

Thanks!

ljCharlie