javascript - 如何显示上传带有不需要的扩展名的文件的错误?

标签 javascript php mysql database file-upload

我正在编写一个脚本,如果所有条件都成立且一切正常,则该脚本会将文件上传到特定文件夹并将数据提交到数据库。该脚本检查文件大小,如果文件大小超过要求,则会显示错误(工作正常)。

该脚本还会检查上传的文件是否具有所需的扩展名,如果正常,则上传该文件,如果它具有不需要的扩展名,则不会上传(工作正常),但如果该文件不是预期的扩展名,则它还应该显示错误。

例如,如果有人上传 .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";
            }
        }
        ?>

关于javascript - 如何显示上传带有不需要的扩展名的文件的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706337/

相关文章:

phpmyadmin - 聚合 javascript 文件的 PHP 文件正在添加不可打印的字符

php - CODEIGNITER 当 user_ID 登录时如何将 user_ID 插入到 mySQL

php - 为什么同一个结果会循环多次?

javascript - tabindex命令效率不高?

javascript - 使用函数从数组中查找最小值和最大值

javascript - 如何避免 Chrome 在自动填充输入上隐藏背景图像和颜色?

javascript - 如何在调整 View 大小后检索图像的高度和宽度

php - 在 mysql 中获取特定周的产品计数

在 Ubuntu 16.04 LTS 上设置 LAMP 后,PHP 无法看到正在上传的文件

mysql - 基于日期和用户的键值 SQL 查询?