改名后的文件名从上传到数据库(post renamed filename from upload t

2019-06-24 16:41发布

我承担责任:水灾真的可以使用一些帮助。 我需要发布文件到数据库的新名称。 目前,该代码被发布的原单名到数据库? 我怎样才能发布新的名称到数据库? 请参阅下面的代码。

<form action="" method="post" enctype="multipart/form-data">
<p>
    <label for="file">Name:</label>
    <input type="text" name="user_name" id="name" size="24"/> 
</p>
<p>
    <label for="file">Email Address:</label>
    <input type="text" name="email" id="email" size="24"/> 
</p>
<p>
    <label for="file">Phone Number:</label>
    <input type="text" name="phone" id="phoneNumber" size="24"/> 
</p>
<p>
    <label for="file">Comments:</label>
    <textarea cols="80" rows="6" name="comments"></textarea> 
</p>
<p>
    <label for="file">Filename:</label>
    <input type="file" name="fupload" id="fupload" size="24"/>
</p>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<br>
<br>
<?php
    require 'config.php';
    require 'functions.php';

    if(isset($_FILES['fupload'])) {
        if(preg_match('/[.](jpg)|(gif)|(png)$/',       $_FILES['fupload']['name'])) {
            $old_filename = $_FILES['fupload']['name'];
            $random_digit=rand(0000,9999);
            $filename=$random_digit . $old_filename;
            $source = $_FILES['fupload']['tmp_name'];
            $target = $path_to_image_directory . $filename;

            move_uploaded_file($source, $target);

            createThumbnail($filename);
        }
    }

    include "connect.php";
    $db_database = new mysqli($db_host, $db_user, $db_pass, $db_database);

    $user_name = stripslashes($_POST['user_name']);
    $email = stripslashes($_POST['email']);
    $phone = stripslashes($_POST['phone']);
    $comments = stripslashes($_POST['comments']);
    $filename = basename( $_FILES['fupload']['name']);

    $query = "INSERT into upload
          (user_name, email, phone, comments, fupload, date_added)
          values ('$user_name', '$email', '$phone', '$comments', '$filename', NOW())";
    $db_database->query($query);
?>

Answer 1:

您需要调整行:

$filename = basename( $_FILES['fupload']['name']);

您创建的SQL语句之前; 你应该设置的值$filename没有到文件的新名称。

哦,我敢肯定,有人将沿着很快指出,你的脚本是开放的SQL注入。



文章来源: post renamed filename from upload to database