如何显示上传带有不需要的扩展名的文件的错误
How to display error for uploading file with unwanted extension?
我正在编写一个脚本,该脚本将文件上传到特定文件夹,并在所有条件都为真且一切正常的情况下将数据提交到数据库。该脚本检查文件大小,如果文件大小超过要求,则显示错误(工作正常)。
该脚本还会检查上传的文件是否具有所需的扩展名,如果正常,则上传文件,如果它是不需要的扩展名,则不会上传(工作正常),但如果文件不是预期的扩展名,那么它也应该显示错误。
例如,如果有人上传.exe或zip或mp3或任何文件,那么它应该显示"无效的文件类型。只允许使用JPG,PNG,GIF,JPEG,PDF和DOC文件。这就是我面临错误的地方。如何显示此消息?我应该放什么代码,放在哪里?
这是我的脚本。
<?php error_reporting(0);
include'db.php';
if(isset($_POST['submit'])!=""){
$extension = substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.'));
$extension = strtolower($extension);
if( $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" ||$extension == ".png" ||$extension == ".pdf" ||$extension == ".doc" ||$extension == ".docx" )
{
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
$limit_size=512000; // Define file size limit in Bytes.
$size_in_kb=1024; // File size in KB
$divide=$limit_size/$size_in_kb; // Dividing both the variables to get the size in KB.
if($size > $limit_size){
echo "<center>Your file size is over limit. Max upload size $divide KB.</center><BR>";
echo "<center><a href='form.php'>Try Again</a></center>";
}
else {
move_uploaded_file($temp,"admin/files/".$name);
$insert=mysql_query("insert into upload(name, fname, phone, email, message)values('$name','$_POST[fname]','$_POST[phone]','$_POST[email]','$_POST[message]')");
}
if($insert){
echo "<center><BR>Data submitted successfully.</center>";
}
else{
die(mysql_error());
}
}
}
?>
<html>
<head>
<title>Upload and Download</title>
</head>
<body>
<style>
h1 {font-family:Georgia, "Times New Roman", Times, serif; font-size:36px; color:#000000}
.formdesign {width: 350px; height: 300px; border:1px solid black; border-radius: 5px; margin-top: 75px; box-shadow: 10px 10px 5px #888888;}
.testbox {width:300px; height: 50px; border: 1px solid grey}
</style>
<center>
<div class="formdesign">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" name="form">
<table style="padding:7px; line-height:1;">
<tr>
<th><label for="fname">Name</label></th>
<td><input type="text" name="fname" id="fname" required maxlength="30" style="width: 220px; height:30px; font-size: 14px; font-family: georgia; text-indent: 15px;" placeholder="Your First Name"/></td>
</tr>
<tr>
<th><label for="phone">Phone</label></th>
<td><input type="text" name="phone" id="phone" required maxlength="15" style="width: 220px; height:30px; font-size: 14px; font-family: georgia; text-indent: 15px;" placeholder="Your Phone Number"/></td>
</tr>
<tr>
<th> <label for="email">Email</label></th>
<td> <input type="text" name="email" style="width: 220px; height:30px; font-size: 14px; font-family: georgia; text-indent: 15px;" placeholder="Your Email ID">
</td>
</tr>
<tr>
<th><label for="message">Message</label></th>
<td> <textarea name="message" rows="4" cols="25" placeholder="Your message here!" maxlength="200">
</textarea> </td>
</tr>
</table><table border="0" cellspacing="0" cellpadding="5" id="table">
<tr>
<th >Chosse Files (Max 500KB)</th>
<td ><label for="photo"></label><input type="file" name="photo" id="photo" /></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="submit" id="submit" value="Submit" /></th>
</tr>
</table>
</form>
</div></center>
<br />
<br />
</body>
</html>
您省略了扩展不正确的else { }部分
<?php error_reporting(0);
include'db.php';
if(isset($_POST['submit'])!="")
{
$extension = substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.'));
$extension = strtolower($extension);
if( $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" ||$extension == ".png" ||$extension == ".pdf" ||$extension == ".doc" ||$extension == ".docx" )
{
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
$limit_size=512000; // Define file size limit in Bytes.
$size_in_kb=1024; // File size in KB
$divide=$limit_size/$size_in_kb; // Dividing both the variables to get the size in KB.
if($size > $limit_size)
{
echo "<center>Your file size is over limit. Max upload size $divide KB.</center><BR>";
echo "<center><a href='form.php'>Try Again</a></center>";
}
else
{
move_uploaded_file($temp,"admin/files/".$name);
$insert=mysql_query("insert into upload(name, fname, phone, email, message)values('$name','$_POST[fname]','$_POST[phone]','$_POST[email]','$_POST[message]')");
}
if($insert)
{
echo "<center><BR>Data submitted successfully.</center>";
}
else
{
die(mysql_error());
}
}
else
{
echo " wrong file type";
}
}
?>
只需做同样的事情,而不是当你抓住太大的尺寸时。在您的其他情况下,获取相同的代码和另一条错误消息,例如上传了错误的文件,只有这些格式......
您只需要填写一个包含错误的数组并将其显示在表单顶部即可。未经测试,但您的代码应该或多或少类似于此示例。
<?php
$allowedExt = array("jpg", "pdf"); // Add others allowed extenstions here
$errors = array();
if( !in_array($extension, $allowedExt)
{
$errors[] = "Invalid file ext";
}
else {
// Normal process
}
?>
<!--Inside the "View" at the top of form show errors-->
<div class="formdesign">
<?php
if( !empty($errors)) :
foreach($errors as $error) :
echo "<p>{$error}</p>";
endforeach;
endif;
?>
</div>
编辑"文件大小"条件,并替换:
$allowed_ext = explode(",", 'jpg,jpeg,gif'); //Extensions separated by coma
if($size > $limit_size){
echo "<center>Your file size is over limit. Max upload size $divide KB.</center><BR>";
echo "<center><a href='form.php'>Try Again</a></center>";
} else if(!in_array(pathinfo($name, PATHINFO_EXTENSION), $allowed_ext)) {
echo "<center>Your file is not allowed.</center><BR>";
echo "<center><a href='form.php'>Try Again</a></center>";
}
您只需替换此代码:
if($size > $limit_size){
echo "<center>Your file size is over limit. Max upload size $divide KB.</center><BR>";
echo "<center><a href='form.php'>Try Again</a></center>";
}
对不起,我的英语不好。
再见
相关文章:
- 用于检查文件扩展名不工作的Javascript If语句
- chrome扩展名“sendResponse”不起作用
- 可以't在不提供文件扩展名的情况下导入TypeScript模块
- Chrome 扩展 WebSQL - 需要背景和内容页面才能访问相同的数据库,但它们不需要
- jQuery 示例:在文件扩展名不允许的情况下重置输入文件元素
- 使用交叉骑手时,使用多个文件而不是一个扩展名.js文件
- 使用 JavaScript 下载不带扩展名的文件
- 如何显示上传带有不需要的扩展名的文件的错误
- 上传 - HTML5 版本 文件扩展名不起作用
- 当
SRC 中的文件扩展名与实际格式不同时的含义
- jQuery检查文件扩展名在IE中不起作用
- 找不到Javascript文件,无故添加了文件扩展名%22
- Chrome创建扩展名以避免源文件//是访问控制不允许的允许源文件
- 当一个JS文件需要另一个没有. JS扩展名的JS文件时,出现404
- 为什么我的功能不显示文件扩展名
- 对js文件进行gzip压缩会创建一个归档文件,而不是在文件输出上附加gz扩展名
- 如何设置img src而不知道Javascript中的文件扩展名
- 如何在AngularJS中跳过ui-router's ui-view并重定向不带.html扩展名的部分url
- 在浏览器中使用Node.JS显示json文件,不带文件扩展名
- 使用Javascript,我需要搜索url的文件扩展名,但它不包括我的搜索中的句点