Text field that has a browse button for local file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Text field that has a browse button for local file

Post 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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the filename should be stored in the

Code: Select all

$_FILESї'yourfileupload']ї'name']
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 :))
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

correct, Javascript doesn't have access to writing into the field either.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Follow the examples here.
Note the <form enctype="multipart/form-data" ..... part of the form.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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?
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ?
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Well, tha'ts a nice idea. I'll give that a try.

Thanks!

ljCharlie
Post Reply