Php 仅上载txt文件
如何检查以便只将.txt文件上载到服务器,而不是php中的其他文件Php 仅上载txt文件,php,file,upload,text-files,Php,File,Upload,Text Files,如何检查以便只将.txt文件上载到服务器,而不是php中的其他文件 if(preg_match('\.txt$', $filename)) 如果文件以.txt结尾,则返回true。如果您只想检查扩展名是否为“.txt”,并且该文件是否为真实文本文件无关紧要,则执行以下操作: $fileName = ... $nameLength = strlen($fileName); if ($nameLength > 4 && substr($fileName, $nameLengt
if(preg_match('\.txt$', $filename))
如果文件以.txt结尾,则返回true。如果您只想检查扩展名是否为“.txt”,并且该文件是否为真实文本文件无关紧要,则执行以下操作:
$fileName = ...
$nameLength = strlen($fileName);
if ($nameLength > 4 && substr($fileName, $nameLength - 4) == '.txt')
{
// Extension is ".txt".
}
else
{
// Other extension or no extension at all.
}
您可以测试文件类型:
if ($_FILES['file']['type'] == 'text/plain') // this file is TXT
此外,还可以使用函数验证文件的mime类型。如果要检查实际的文件mime类型,请尝试PHP。(请参见该页上的示例#1。如果返回的字符串不是“text/html”,则它不是文本文件。)
编辑:请记住该功能已被折旧。改用finfo_文件。签出我写的这段代码行吗
<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."; }
}
?>
Välj din txt文件:
使用
比如说:
$filename = pathinfo($_FILES['upload']['name']);
$ext = $filename['extension'];
if($ext !== 'txt'){
//don't upload
}else{
//upload
}
上传序列号文件:
但它可能是一个带有不同扩展名的JPG文件。任何其他方法都需要信任用户或先上传整个文件才能找到答案。此外,尽管有mime类型,大多数浏览器还是会将其作为文本文件打开。上传后,使用我发布的mime_content_type函数。使用preg_match而不是mime type来匹配文件类型不是更安全吗?@www139我会说不,因为preg_match将查看其中的文本,而且像.gif这样的扩展名很容易被伪造,或者像那样保存文件,这会破坏preg_match提供的结果。检查mime类型不是一个完美的解决方案,但仅扩展本身是一个糟糕的解决方案check@Alexandergirnt-@Jake是对的,请不要假设*.txt与纯文本文件相同,finfo_文件是PHP5.3及更新版本中唯一的标准文件。在此之前,您需要对其进行PECL扩展。
<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);
}
?>