Results 1 to 4 of 4

Thread: Get path of selected file

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

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

  3. #3

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

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

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width