简单的PHP上传,我如何使一个自动增量名称和设置允许类型和提高安全性在应用程序级别


simpel php upload, how i can make an auto increment name and set allow type and improve security in applicaiton level

这是上传文件并将值插入mysql的表单,数据库:

id -------- auto increment
name-------
filename---

文件结构:

/database.php----------the pdo database connection
/upload.php------------this will be the forme
/upload_class.php------this will be the class to upload
/filefolder------------this is the folder where file will be upload
连接函数(database.php):
class Database {
    protected $host='localhost';
    protected $user='root';
    protected $db = 'dms';
    protected $pass = '';
    protected $conn;
    public function __construct(){
        $this->conn = new PDO("mysql:host=localhost;dbname=dms","root","");
        $this->conn->exec("SET CHARACTER SET utf8");
    }
}

连接后,我将创建一个表单(upload.php)来插入并发送值给insert函数:

<?php
     require_once('./upload_class.php');
    if (isset($_POST['insert'])) {
        $uploadclass-> name= $_POST['name'];
        $uploadclass-> filename= $_FILES['filename']['name'];
        $uploadclass-> insert();
    }
    ?>
    <form action="" method="post" enctype="multipart/form-data">
        <table border="0" class="form_au">
            <tr><td>nameه</td>
                <td><input type="text" class="input1" name="name" /></td>
            </tr>
            <tr><td>filetoupload</td>
                <td><input name="filename" type="file"></td>
            </tr>
            <tr><td colspan="2"><input type="submit" name="insert" class="button" value="upload"/></td></tr>
       </table>
    </form>

现在这里是上传类(upload_class.php),它将检查并上传文件到名为(file_folder)的文件夹,并在文件上传成功时将值插入数据库。

<?php
    include ('detabase.php');
    class Uploadclass extends database{
        public function insert() {
                $targetFLE = "./filesfolder";
                $targetFLE = $targetFLE . basename( $_FILES['filename']['name']);
                $sql = "INSERT INTO dab_din (name,filename,) VALUES (:name,:filename)";
                $result = $this -> conn -> prepare($sql);
                if(move_uploaded_file($_FILES['filename']['tmp_name'], $targetFLE)) { 
                    $query = $result -> execute(array(":name"   =>$this -> name,
                                                        ":filename"     => $_FILES['filename']['name']));
                    if($query){echo $message = "success and file uploaded";} else {echo "failed on insert to dabas";}
                 } else { $message = "please select file";}
            }
        }
        $uploadclass = new Uploadclass();

    ?>

注意:我没有设置安全性和验证,所以我可以快速回答我的问题,但我在现实中有,所以问题只是在上传部分

我的问题:

1-文件上传,名称应更改为(file_1, file_2, file_3),然后上传到文件夹,对于解决方案,我发现创建一个新的选择基于以前的名称被添加到数据库,但它会使应用程序工作缓慢。和重复的机会,因为会有很多使用上传在同一时间,所以如果在一个确切的时间将会给结果相同的值,有选择。那么我如何改变名字自动递增而不是随机的名字

2-如何设置类型,使其更安全仅在上传部分,只允许pdf

3-在应用端什么是php最重要的安全步骤,否则类型赋值。

To 1:

最简单的解决方案是从数据库中选择MAX:

最高的ID。
$id = $db->GetOne("SELECT MAX(id) AS id FROM database");
$filename = $_FILES['filename']['name'] . "_" . $id;

另一个解决方案是将最大值单独保存在某个地方,并在每次上传pdf时增加它。这样,您就不必遍历整个表。