如何将文件上传到我的服务器并在我的数据库

时间:2015-08-13 04:42:19

标签: php database image

我设计了一个汽车销售网站。

我有一个表单,其中输入了有关汽车的某些信息,然后将其添加到数据库并显示在我的主表单上。如何将图像发送到服务器,然后使用URL作为图像的源。

干杯。

这是我的代码。

PHP:

<?php

try {
    # Connect to SQLite database
    $dbh = new PDO("sqlite:../Car_Sales_Network");

    $make = $_POST['Make'];
    $model = $_POST['Model'];
    $badge = $_POST['Badge'];
    $price = $_POST['Price'];
    $trans = $_POST['Transmission'];
    $ppl = $_POST['P_Plate_Legal'];

    print_r($_POST);

    $sth = $dbh->prepare('INSERT INTO Cars_On_Network
                         ("car_make","car_model","car_badge","price","trans","P_Plate_Legal")
                         VALUES
                         (?, ?, ?, ?, ?, ?)');

    $sth->execute(array($make, $model, $badge, $price, $trans, $ppl));

    header("Location: ../Carsales_Network.php");


}



catch(PDOException $e) {
    echo $e->getMessage();
}

?>

HTML:

<!DOCTYPE html>

<html>
<head>
    <title>New Vehicle</title>
    <link type="text/css" rel="stylesheet" href="New_Car_Form.css"/>
</head>

<body>
        <div id="main">
        <form action="Insert_Car.php" method="post" enctype="multipart/form-data">
        Make:<br>
        <input type="text" name="Make">
        <br>
        Model:<br>
        <input type="text" name="Model">
        <br><br>
        Badge:<br>
        <input type="text" name="Badge">
        <br>
        Price:<br>
        <input type="text" name="Price">
        <br>


        Transmission: <br>
        <input type="radio" name="Transmission" value="Manual" checked>Manual
        <br>
        <input type="radio" name="Transmission" value="Auto">Automatic
        <br><br>

        <form enctype="multipart/form-data" action="upload_file.php" method="post">
        P Plate Legal: <br>
        <select name="P_Plate_Legal">
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select>
        <br>
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">

        </form>

        <br>
        <br>
        <input class="submit" type="submit" value="Submit">



        <br>
        <a href="http://1673-itstudies/12-infotech/jsummers/Carsales_Network.php" class="myButton">Let's go back!</a>


        <br>







    </div>

</body>
</html>

</body>
</html>

如何将上传的图像发布到要使用的数据库中,我的数据库中有一个文件夹,上面有汽车照片。

1 个答案:

答案 0 :(得分:0)

将文件上传到php中的服务器,使用php fileupload函数

move_uploaded_file ( string $filename , string $destination )

示例:

<?php
  $uploads_dir = '/uploads';
  foreach ($_FILES["fileToUpload"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["fileToUpload"]["tmp_name"][$key];
    $name = $_FILES["fileToUpload"]["name"][$key];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
   }
  }
?>

供参考检查move_uploaded_file()