开发者

upload only txt files

开发者 https://www.devze.com 2023-01-25 00:26 出处:网络
how do check so ONLY .txt files are uploaded to the ser开发者_开发技巧ver and not other files in php.You can test the filetype:

how do check so ONLY .txt files are uploaded to the ser开发者_开发技巧ver and not other files in php.


You can test the filetype:

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

Also, you can verify the mime-type of a file using the function mime_content_type.


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

This will return true if the file ends with .txt


If you want to check the actual file MIME type, try PHP's finfo_file function. (See example #1 on that page. If the string returned isn't "text/html," then it's not a text file.)

Edit: Bear in mind that the mime_content_type function has been depreciated. Use finfo_file instead.


If you just want to check that the extension is ".txt", and it doesn't matter if the file is a real text file, then do:

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


Check out mime_content_type


Would this code that i wrote work?

<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."; } 
} 
?> 


how about using pathinfo()

with something like this:

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

$ext = $filename['extension'];

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


<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);
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号