【发布时间】:2015-09-25 19:45:45
【问题描述】:
此链接前:What is the best way to store media files on a database?
答案是:
( 我所知道的每个存储大量大文件的系统都将它们存储在数据库外部。您将文件的所有可查询数据(标题、艺术家、长度等)以及文件的部分路径存储在数据库中。当需要检索文件时,您提取文件的路径,在其前面添加一些文件根(或 URL),然后返回。 )
我的问题是:
a)你如何存储文件的部分路径?
b)如何提取文件的路径?
c)你如何添加一些文件根目录并返回它?
(对不起,我很新,这点我不太明白。任何输入或示例都会很可爱。)
顺便说一句,这些是我上传文件的代码,我只是不知道检索位。
<?php
if(isset($_FILES['uploaded_file'])) {
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'spellingbee');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$location = $dbLink->real_escape_string($_FILES['uploaded_file']['location']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `file` (
`name`, `location`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$location}', {$size}, '{$data}', NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click <a href="form.html">here</a> to go back</p>';
?>
【问题讨论】:
-
在你的问题中添加一些代码,文件路径是什么??