如何在数据库中保存文件路径并在详细页面中显示路径

时间:2016-03-02 20:13:48

标签: php mysql file-upload

我正在忙于一个小项目,人们可以填写一个表格,将值保存到数据库中。现在一切都工作正常,文件上传路径。在我的数据库中,我有三个表人,地址和简历(有关系)见图:

picture one

现在我希望路径为www.test.nl/directory/filename,这样我就可以创建一个带链接的按钮,这样当你按下按钮时它会下载文件。在我的详细信息页面上,你可以看到它显示的是cv_id,而不是路径。我的文件上传是一个单独的文件,我的人和地址上传见下文:

地址和人员上传:

<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";

// CREATE A CONNECTION WITH THE DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

// ADDRESS APPEND - PREPARE SQL STATEMENT AND BIND PARAMS
// ADRES TOEVOEGEN - BEREID SQL STATEMENT EN BIND PARAMS
$stmt = $conn->prepare("INSERT INTO address (address_street, address_housenumber, 
                                         address_zipcode, address_city, address_state)
                    VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $straat, $huisnummer, $postcode, $stad, $provincie);

$straat = htmlspecialchars($_POST['straat']);
$huisnummer = htmlspecialchars($_POST['huisnummer']);
$postcode = htmlspecialchars($_POST['postcode']);
$stad = htmlspecialchars($_POST['stad']);
$provincie = htmlspecialchars($_POST['provincie']);

// EXECUTE STATEMENT
// STATEMENT UITVOEREN
$result = $stmt->execute();    
if ($result === FALSE) {
die("Error: " . $stmt->error);
}

// CAPTURE LAST INSERTED address_id
// PAK DE LAATST INGEVOERDE address_id
$last_id = $conn->insert_id;

// PERSON APPEND - PREPARE SQL STATEMENT AND BIND PARAMS
// PERSOON TOEVOEGEN - BEREID SQL STATEMENT EN BIND PARAMS
$stmt = $conn->prepare("INSERT INTO person (person_firstname, person_lastname, 
                                        person_email, person_phonenumber,
                                        person_cv, person_address)
                     VALUES (?, ?, ?, ?, ?, ?)");
 $stmt->bind_param("sssssi", $firstname, $lastname, $email, $telephone, $cv, $last_id);

$firstname = htmlspecialchars($_POST['firstname']);
$lastname = htmlspecialchars($_POST['lastname']);
$email = htmlspecialchars($_POST['email']);
$telephone = htmlspecialchars($_POST['telephone']);

// EXECUTE STATEMENT
// STATEMENT UITVOEREN
$result = $stmt->execute();    
if ($result === TRUE) {
$URL="http://localhost:8080/Website/bedankt.php";  
header ("Location: $URL");  
} else {
    echo "Error: " . $stmt->error;
}

// CLOSE CONNECTION AND STATEMENT
// SLUIT CONNECTIE EN STATEMENT
$stmt->close();
$conn->close();
?>

file upload.php

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "usbw";
 $dbname = "persons";

// CREATE A CONNECTION WITH THE DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['submit']))
{
  $filetmp = $_FILES["cv"]["tmp_name"];
  $filename = $_FILES["cv"]["name"];
  $filetype = $_FILES["cv"]["type"];
  $filepath = "files/".$filename;

  move_uploaded_file($filetmp,$filepath);

  $sql = "INSERT INTO cv (cv_name,cv_path,cv_type) VALUES ('$filename','$filepath','$filetype')";
  $result = mysqli_query($conn, $sql);
}
$cv = $conn->insert_id;
?>

我的详细信息页面:

<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT person_firstname, person_lastname, 
           person_email, person_phonenumber, person_cv,  
           address_street,address_housenumber, 
           address_city,address_state,address_zipcode 
    FROM person 
       inner join address on address.address_id = person.person_address";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table border=1>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Straat</th>
<th>Huisnummer</th>
<th>Postcode</th>
<th>Stad</th>
<th>Provincie</th>
<th>Email</th>
<th>Mobiel</th>
<th>CV</th>
</tr>";


while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row["person_firstname"] . "</td>";
    echo "<td>" . $row["person_lastname"] . "</td>";
    echo "<td>" . $row["address_street"] . "</td>";
    echo "<td>" . $row["address_housenumber"] . "</td>";
    echo "<td>" . $row["address_zipcode"] . "</td>";
    echo "<td>" . $row["address_city"] . "</td>";
    echo "<td>" . $row["address_state"] . "</td>";
    echo "<td>" . $row["person_email"] . "</td>";
    echo "<td>" . $row["person_phonenumber"] . "</td>";
    echo "<td>" . $row["person_cv"] . "</td>";
    echo "</tr>";
}
} else {
echo "Er is niks in het database gevonden";
}
$conn->close();
?>

我希望你们可以帮助我,我是PHP的新手,所以我还在学习这一切。请注意,一切运行良好...请上传www.test.nl/directory/filename并将其保存到我的表cv_path中......在详细信息页面中显示路径(可能是一个按钮)。如果有一种方法使用与地址几乎相同的语句来将文件上传到cv而不是那么好。但是因为这现在正在运行(到目前为止)我保持文件上传和人员和地址上传分离。如果可以在CV下的详细页面中添加下载按钮,那将下载完美的文件。

2 个答案:

答案 0 :(得分:1)

使用此功能,您可以在数据库中存储文件名

$filetmp = $_FILES["cv"]["tmp_name"];

获取您的图片或文件

<img scr="<?=base_url?>/uploads/<?=your file name in DB?>">

就是这样

答案 1 :(得分:1)

您必须更改SELECT查询以获取cv_path而不是cv_id。

然后您可以使用php将其定义为$cv_file = "www.test.nl/" . $row['cv_path'];

因此echo $cv_file;将为您提供www.test.nl/files/test.pdf

现在您只需创建一个链接。 echo "<a href='http://" . $cv_file . "'>cv file</a>";

同样的事情是执行相同的SELECT查询,但将其添加到链接中:

echo "<a href='http://www.test.nl/" . $row['cv_path'] . "'>cv file</a>";

他们都会创建一个链接,将您带到http://www.test.nl/files/test.pdf

这些确实是基础知识,你不应该问这么简单的问题,因为它们已经多次在堆叠上回答。

EDIT。 至于按钮,如果你想使用纯HTML,你可以像BalusC在这里说的那样:How to create an HTML button that acts like a link?

在你的情况下会是这样的:

echo "<form action='http://" . $cv_file . "'>";
echo "<input type='submit' value='Go to cv'>";
echo "</form>";