仅上传txt文件

时间:2010-11-15 19:23:43

标签: php file upload text-files

如何检查只有.txt文件上传到服务器而不是php中的其他文件。

8 个答案:

答案 0 :(得分:8)

您可以测试文件类型:

if ($_FILES['file']['type'] == 'text/plain') // this file is TXT

此外,您可以使用函数mime_content_type来验证文件的mime类型。

答案 1 :(得分:1)

if(preg_match('\.txt$', $filename))

如果文件以.txt

结尾,则返回true

答案 2 :(得分:1)

如果要检查实际的文件MIME类型,请尝试PHP的finfo_file function。 (参见该页面上的示例#1。如果返回的字符串不是“text / html”,那么它不是文本文件。)

编辑:请记住mime_content_type函数已被折旧。请改用finfo_file。

答案 3 :(得分:0)

如果您只是想检查扩展名是否为“.txt”,并且该文件是否为真实文本文件无关紧要,请执行以下操作:

$fileName = ...
$nameLength = strlen($fileName);
if ($nameLength > 4 && substr($fileName, $nameLength - 4) == '.txt')
{
    // Extension is ".txt".
}
else
{
    // Other extension or no extension at all.
}

答案 4 :(得分:0)

答案 5 :(得分:0)

我写的这段代码会起作用吗?

<form enctype="multipart/form-data" action="upload.php" method="POST"> 
Välj din txt fil: <input name="uploaded" type="file" /><br /> 
<input type="submit" value="Upload" /> 
</form> 


<?php 
$target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1;

if ($uploaded_size > 350000) { 
echo "Your file is too large.<br>"; $ok=0; 
}

if ($uploaded_type !=="text/plain") { 
echo "Only txt files allowed<br>"; $ok=0; 
} 

if ($ok==0) { 
echo "Sorry your file was not uploaded"; 
} else { 
            if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { 
                     echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
            } else { echo "Sorry, there was a problem uploading your file."; } 
} 
?> 

答案 6 :(得分:0)

如何使用pathinfo()

有这样的事情:

$filename = pathinfo($_FILES['upload']['name']);

$ext = $filename['extension'];

if($ext !== 'txt'){
  //don't upload
}else{
  //upload
}

答案 7 :(得分:0)

<strong>Upload Serial Number File:</strong>
               <input type="file" name="fileToUpload" id="fileToUpload"><br><br>  




<?php


// Pass the input field name , file upload success it return TRUE.   
function Get_the_Client_UploadFile($InputfeildName)
{


  $ClientUserName   = gethostbyaddr($_SERVER['REMOTE_ADDR']);   
  $Cient_Uploadfile = "";   // do not remove

$SerialNumberFileuploadOk = FALSE;


    $uploaddir =  'UserInputLog/' ;
    if( is_dir($uploaddir) === false )
    {
        mkdir($uploaddir);
    }


    $Cient_Uploadfile = basename($_FILES[$InputfeildName]['name']);
    if(!empty($Cient_Uploadfile))
    {     


               $Cient_Uploadfile = $uploaddir . $ClientUserName.'_'.basename($_FILES[$InputfeildName]['name']);              
                 //delte old old uplaoded file from logs
              if (file_exists($Cient_Uploadfile))               
                  unlink($Cient_Uploadfile);    //delete     
              //copy here 
              if (move_uploaded_file($_FILES[$InputfeildName]['tmp_name'], $Cient_Uploadfile)) {
                  Data_Log("File is valid, and was successfully uploaded . $Cient_Uploadfile");
                  $SerialNumberFileuploadOk = TRUE;   
              } else {
                  DisplayMessageDialog("unable to upload file = $Cient_Uploadfile"); 
              }
              //print_r($_FILES);          

               // Allow certain file formats

              $FileType = pathinfo($Cient_Uploadfile,PATHINFO_EXTENSION);
              if($FileType != "txt") {
                  DisplayMessageDialog("File Ext ERROR: $FileType Please upload only '.txt' file (Notepad file with serial number)");
                    if (file_exists($Cient_Uploadfile))               
                      unlink($Cient_Uploadfile);    //delete  
                  $SerialNumberFileuploadOk = FALSE;
                 }

             // Check file size
             //we want to check the size of the file. If the file is larger than 5 MB
             if ($_FILES[$InputfeildName]["size"] > 5000000) {
                 DisplayMessageDialog( "Sorry, your file is too large. Allowed only 5 MB");
                  if (file_exists($Cient_Uploadfile))               
                      unlink($Cient_Uploadfile);    //delete  
                 $SerialNumberFileuploadOk = FALSE;
                 }
    }

    if($SerialNumberFileuploadOk == FALSE)
    $Cient_Uploadfile = "";

    return  $SerialNumberFileuploadOk;

}

function DisplayMessageDialog($msg)
{
  echo '<script type="text/javascript">alert("' . $msg . '")</script>';
  Data_Log(" *** DialogBox Open *** :  ".$msg);
}
?>