|
-
Sep 20th, 2006, 01:08 AM
#1
Thread Starter
PowerPoster
Get path of selected file
I've got an option where a user can browse for a file using the code below.
What I want to know is how to get the full path of the file selected?
PHP Code:
if ($_POST['submit'] == "") {
?>
<form enctype="multipart/form-data" action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post">
File:<br>
<input type="file" name="file1" size="75"></p>
<p><input type="submit" value="Post" name="submit"></p>
</form>
<?
}
else {
$file = $_FILES['file1']['name'];
//$file = test.txt
//I want $file = C:\test.txt etc...
echo "File to upload is ($file)";
}
Also, is there a way to restrict the type of files that are displayed when the "browse" button is clicked? eg. only .txt or .csv etc...
Last edited by lintz; Sep 20th, 2006 at 01:11 AM.
-
Sep 20th, 2006, 01:30 AM
#2
Re: Get path of selected file
You cannot get the path to the file on the client machine; it is never sent. Only the file itself is sent.
Use the accept attribute to specify a comma-delimited list of MIME types to allow.
HTML Code:
<input type="file" accept="text/plain,text/csv" ... >
-
Sep 20th, 2006, 01:45 AM
#3
Thread Starter
PowerPoster
Re: Get path of selected file
Thanks Pengate but using the below doesn't restrict to only CSV files?
HTML Code:
<input type="file" name="file1" size="75" accept="text/csv">
-
Sep 20th, 2006, 03:25 AM
#4
Re: Get path of selected file
Evidently it's not very well supported then. 
Look at it this way: You basically can't enforce rules on the user agent. The best you can do is offer hints - tell them what they are supposed to do. Whether they take notice of those hints is up to them; basically, depending on whether they support those features or not.
That's how the web works, and also why client-side validation can never replace server-side validation.
Don't lose faith though - the best web pages can contain tons of information that is not used by any user agent today, but may be in the future. These are the future-proof ones, the ones that should always behave the way they are supposed to. The "no-one supports it so I won't use it" mentality simply leads to pages that require constant maintenance.
Last edited by penagate; Sep 20th, 2006 at 03:29 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|