PDA

Click to See Complete Forum and Search --> : Get path of selected file


lintz
Sep 20th, 2006, 01:08 AM
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?

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...

penagate
Sep 20th, 2006, 01:30 AM
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.

<input type="file" accept="text/plain,text/csv" ... >

lintz
Sep 20th, 2006, 01:45 AM
Thanks Pengate but using the below doesn't restrict to only CSV files?

<input type="file" name="file1" size="75" accept="text/csv">

penagate
Sep 20th, 2006, 03:25 AM
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.