简体   繁体   中英

html: how to restrict upload file format to plain text

I need to have a upload file functionality on my web page. I only want people to be able to choose .csv and plain text(.txt) file.

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

I know the attribute accept may work. But there are two problems.

  1. It doesn't work with Firefox or Safari
  2. csv works fine, but text seems not restricted. I can upload .java file, .rtf file, .python file, etc..

Anyone knows how to implement that ?

Be mindful that anything that you do on the client side, for example using the accept attribute in the input tag, can easily be ignored/overridden by the client. So, if you really want to prevent files other than the types that you specify from being uploaded to your server, then you should do some sort of checking on the server side, in the script that your form posts to.

如果您只想接受txt文件,请将您的接受从text / plain更改为accept=".csv,.txt"

$target="images/";  //path to upload
if( isset($_POST['submit']) ) //check if post is set
{
        if $_FILES['file']['type'] != 'text/plain'
        {  
             // error
         } else {
           //upload
             move_uploaded_file($_FILES['file']['tmp_name'], $target);
          }   

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM