将具有唯一ID的文件名从新文件夹移动到SQL数据库

时间:2014-09-16 00:59:07

标签: php mysql database

我环顾四周,并没有找到我特别想要的东西。 我有很多文本输入和文件上传的表单。

我已经想过如何上传文件,给它一个唯一的ID并将其放入我的网络服务器文件夹中。非常顺利的航行。但是,我想在我的MySQL数据库中获得那个花哨的新ID。

我已将upload.php页面与文本表单分隔到数据库

<?php

//Connecting and Sending data to the database follows
$dbc = mysqli_connect('localhost', 'root', 'root', 'surfboardhub')
or die('Error connecting to MySQL server');

//Get values from  
$location = "";
$price = "";
$thick = "";
$width = "";
$height ="";
$model = "";
$brand = "";
$email = "";
$category = "";

if(isset($_POST['location'])){ $location = $_POST['location']; }
if(isset($_POST['price'])){ $price = $_POST['price']; }
if(isset($_POST['thick'])){ $thick = $_POST['thick']; }
if(isset($_POST['width'])){ $width = $_POST['width']; }
if(isset($_POST['height'])){ $height = $_POST['height']; }
if(isset($_POST['model'])){ $model = $_POST['model']; }
if(isset($_POST['brand'])){ $brand = $_POST['brand']; }
if(isset($_POST['email'])){ $email = $_POST['email']; }

//if(isset($_POST['image'])){ $imagename = $_POST['imagename']; } 
//if(isset($_POST['mime'])){ $mime = $_POST['mime']; } 

$query = "INSERT INTO uploads (location, price, thick, width, height, model, brand, email,category) 
VALUES ('$location', '$price','$thick','$width','$height', '$model', '$brand', '$email','$category')";

$result = mysqli_query($dbc,$query)
or die('Error querying database.');

mysqli_close($dbc);

然后我将我的位置转到我的网络服务器中的新位置。

$name = $_FILES['image']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$type = $_FILES['image']['type'];

$tmp_name = $_FILES['image']['tmp_name'];

if (isset($name)) {
if (!empty($name)) {
if (($extension=='jpg'||$extension=='jpeg'||$extension=='png'||$extension=="gif")&&$type=='image/jpeg'||$type=='image/png'||$type=='image/gif') {


 $location = 'uploads/';
 $location = $location . uniqid();



   if (move_uploaded_file($tmp_name, $location.$name)) {
  echo 'uploaded!';
    } 

    else {
   echo 'There was an error.';
    }


} else {
echo 'File must be jpg/jpeg, png, or gif.';
}

} else {
echo 'Please choose a file';
}
}
?>

基本上,我需要获取新的唯一ID才能转到文本信息的位置,因为它们都是一次性提交的。我希望能够找出谁上传了什么,如果需要的话。如果它没有唯一的ID,我可以让它工作,但由于某种原因,让那些unqid绊倒我。思考?很有责任。

1 个答案:

答案 0 :(得分:0)

uniqid()保存到PHP变量,然后您可以在多个地方使用它:

首先,创建一个ID:

<?php
    $ID = uniqid();
?>

然后,使用新的$ID变量保存文件:

<?php
    $name = $_FILES['image']['name'];
    $extension = strtolower(substr($name, strpos($name, '.') + 1));
    $type = $_FILES['image']['type'];

    $tmp_name = $_FILES['image']['tmp_name'];

    if (isset($name)) {
        if (!empty($name)) {
            if (($extension=='jpg'||$extension=='jpeg'||$extension=='png'||$extension=="gif")&&$type=='image/jpeg'||$type=='image/png'||$type=='image/gif') {
                $location = 'uploads/';
                $location = $location . $ID;

                if (move_uploaded_file($tmp_name, $location.$name)) {
                    echo 'uploaded!';
                } else {
                    echo 'There was an error.';
                }
            } else {
                echo 'File must be jpg/jpeg, png, or gif.';
            }
        } else {
            echo 'Please choose a file';
        }
    }
?>

然后,将数据保存到数据库,包括$ ID

<?php
    //Connecting and Sending data to the database follows
    $dbc = mysqli_connect('localhost', 'root', 'root', 'surfboardhub')
    or die('Error connecting to MySQL server');

    //Get values from  
    $location = "";
    $price = "";
    $thick = "";
    $width = "";
    $height ="";
    $model = "";
    $brand = "";
    $email = "";
    $category = "";

    if(isset($_POST['location'])){ $location = $_POST['location']; }
    if(isset($_POST['price'])){ $price = $_POST['price']; }
    if(isset($_POST['thick'])){ $thick = $_POST['thick']; }
    if(isset($_POST['width'])){ $width = $_POST['width']; }
    if(isset($_POST['height'])){ $height = $_POST['height']; }
    if(isset($_POST['model'])){ $model = $_POST['model']; }
    if(isset($_POST['brand'])){ $brand = $_POST['brand']; }
    if(isset($_POST['email'])){ $email = $_POST['email']; }

    //if(isset($_POST['image'])){ $imagename = $_POST['imagename']; } 
    //if(isset($_POST['mime'])){ $mime = $_POST['mime']; } 

    $query = "INSERT INTO uploads (ID, location, price, thick, width, height, model, brand, email,category) 
    VALUES ('$ID', '$location', '$price','$thick','$width','$height', '$model', '$brand', '$email','$category')";

    $result = mysqli_query($dbc,$query)
    or die('Error querying database.');

    mysqli_close($dbc);
?>