post重命名文件名从上传到数据库 [英] post renamed filename from upload to database

查看:98
本文介绍了post重命名文件名从上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的使用一些帮助。我需要将该文件的新名称发布到数据库。目前这个代码是将原始名称发布到数据库?如何将新名称发布到数据库?请参阅下面的代码。

I coule really use some help. I need to post the new name of the file to the database. Currently this code is posting the orginal name to the database? How can I post the new name to the database? Please see the code below.

<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);
?>


推荐答案

您需要调整行:

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

您应该将 $ filename 的值设置为文件的新名称。

just before you create the SQL statement; you should set the value of $filename there to the new name of your file.

确保有人会很快指出你的脚本是开放的SQL注入。

Oh, and I'm sure that someone will be along shortly to point out that your script is open to SQL injections.

这篇关于post重命名文件名从上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆