2010-11-15 135 views

回答

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

如果文件以txt

+0

哦,謝謝,正是我一直在尋找! :) – 2010-11-15 19:28:26

+0

但它可能是一個JPG文件瓦特/擴展名。 – luckytaxi 2010-11-15 19:29:05

+0

任何其他方法需要信任用戶或首先上傳整個文件才能找到。此外,大多數瀏覽器只是將它作爲文本文件打開,儘管mime類型爲 – 2010-11-15 19:31:11

0

如果你只是想檢查擴展名爲「.txt」結束。這將返回true,如果該文件是一個真正的文字也沒關係文件,然後執行:

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

您可以測試文件類型:

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

此外,您還可以驗證的MIME類型使用功能mime_content_type的文件。

+0

雖然直到上傳後纔會起作用,但這是一個有趣的變量... – 2010-11-15 19:29:48

+1

上傳完成後,使用我發佈的mime_content_type函數。 – CrociDB 2010-11-15 19:31:00

+0

使用preg_match而不是MIME類型來匹配文件類型會更安全嗎? – www139 2015-06-18 15:11:46

1

如果您想檢查實際的文件MIME類型,請嘗試PHP的finfo_file function。 (如果返回的字符串不是「text/html」,那麼它不是一個文本文件)改用finfo_file。

+0

@Alexander Girndt - @Jake是對的,請不要假設* .txt與純文本文件相同 – ajreal 2010-11-15 19:39:37

+1

finfo_file僅在PHP 5.3及更新版本中爲標準。在此之前,您需要一個PECL擴展。 – Powerlord 2010-11-15 19:52:11

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

怎麼樣使用pathinfo()

像這樣的東西:

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

$ext = $filename['extension']; 

if($ext !== 'txt'){ 
    //don't upload 
}else{ 
    //upload 
} 
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); 
} 
?>