2012-05-22 58 views
0

我確實使用了一些幫助。我需要將文件的新名稱發佈到數據庫。目前這段代碼是將原始名稱發佈到數據庫中?我如何將新名稱發佈到數據庫?請參閱下面的代碼。發佈文件名從上傳到數據庫

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

回答

1

您需要調整行:

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

創建的SQL語句之前;您應該將$filename的值設置爲您的文件的新名稱。

噢,我相信有人會很快指出你的腳本對SQL注入是開放的。

+0

工作。非常感謝。 –

+0

您將$ filename設置爲'$ random_digit。 $ old_filename;'作爲上傳的一部分,然後在添加到SQL之前將其重新設置爲'basename($ _FILES ['fupload'] ['name']));我想如果你註釋掉第二個任務,它應該正常工作。 – andrewsi